views:

1442

answers:

3

Hi all, I'm using this script http://mondaybynoon.com/2009/02/23/creating-custom-form-elements-using-jquery-selects/ to create custom css select boxes in my page. Javascript code that is used to apply the css styles on selectboxes, uses their id as a parameter:

$(document).ready(function() {
 $('#cont').selectbox({debug: true});
});

<select id="cont" name="cont" tabindex="1">
<option value="1">aaa</option>
<option value="2">bbb</option>
<option value="3">ccc</option>
</select>

Can I somehow use just the "name" attribute of select boxes and not the "id"??? Is this possible?

+3  A: 

Sure, to base your selector off of the name-attribute, simply do the following:

$("[name='nameHere']");
Jonathan Sampson
+2  A: 

you can use the attribute selector to selct via name

e.g.

$('select[name="cont"]').selectbox({debug: true});
CeejeeB
thank you all for your quick responses:)The reason why I wanted to change this was because I use some ajax and php to load options into these select boxes.This ajax/php code stopped working after I added the id attribute on the select boxes. I thought that removing the "id" would solve the problem.Eventually it seems that it's not the "id" which causes the problem. For some reason these two things cannot work both at the same time.
ktsixit
Would be interested to get to the bottom of that. That fix sounds like a bit of a fudge
James Wiseman
+2  A: 

This is how you do it

$("[name='cont']");

Combining it with an element selector will make it faster:

$("select[name='cont']");

Though In the example you give, I can't imagine why. The following will be so much faster:

$("#cont");
James Wiseman