Matching when an item has multiple identifiers

I look through the form and display the content corresponding to my selected identifier. The problem is that some divs contain more than one identifier, in which case it stops working. Any ideas? Thanks.

Jquery Code:

$('#myForm').find('div').each(function() { var myId = $(this).attr('id'); /* This will work */ if (myId == "Select1"){ $(this).removeClass("hideMe"); $(this).addClass("showMe"); } /* This does not work */ else if (myId == "Select4"){ $(this).removeClass("hideMe"); $(this).addClass("showMe"); } else{} }); 

HTML code:

 <div class="hideMe" id="Select1"> <p>Some Content</p> </div> <div class="hideMe" id="Select2 Select3 Select4 Select5"> <p>Some Content</p> </div> 
+4
source share
5 answers

There is no such thing as multiple identifiers.

https://developer.mozilla.org/en/XUL/Attribute/id

According to the standard, any string data in the id property is treated as part of the value.

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") .

link: http://www.w3.org/TR/REC-html40/types.html#type-name

There's a different way though! You can have all kinds of class names, and you can use jQuery to grab an element by class name.

HTML

 <div class="hideMe Select1"> <p>Some Content</p> </div> <div class="hideMe Select2 Select3 Select4 Select5"> <p>Some Content</p> </div> 

Javascript

 $('.Select2')[0] 

Part [0] is that when you get elements by class name, there can be several. The jQuery selector returns an array, so you just grab the first one.

+10
source

You cannot have multiple identifiers. However, you can have several classes if you want.

+1
source

It is not valid to have multiple IDs - the browser will see id = "Select2 Select3 Select4 Select5" as one line, but this line will be invalid because it contains spaces.

From the HTML data type specification: http://www.w3.org/TR/REC-html40/types.html#type-name

You should use classes for this, I think.

+1
source

ID is unique , and items can only have 1 ID !

Use multiple classes instead.

0
source

An element must not have more than one unique identifier, so it actually called id: identify it with all the others. In any case, you need to check if myId contains Select4, and not an equality check.

0
source

All Articles