jQuery id Selectors more details
In general the jQuery id selector selects only the first matched element. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element. As you may know a document with more than one element using the same ID is invalid.
You must note that it is not a good html coding habit to assign same ip to more than one html entity.
Take a look at the following example, where we have assigned same id to more than one element, only the first occurence of gets selected.
|
You may like to try this example here.
Notice that the selector
$("#id1")
does not tell if it will apply to a paragraph or another element ( for example an h2 header). It will just match the first occurence of that id to whichever element it belongs to. In the next chapter we will see how to select an id of a particular element.
Take a look at the following example, where we have assigned same id to the <h2> element and the <p> element and the selection applies to the first occurence of the selection - in this case the <h2> element
|
You may like to try this example here.