Bug Tracker

Opened 10 years ago

Closed 10 years ago

#13608 closed bug (notabug)

DOES CHILD SELECTOR WORKS WRONGLY WITH ID

Reported by: royAbhi86 Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.6.3
Keywords: Cc:
Blocked by: Blocking:

Description

$('#parent') : selects the first element in the DOM tree with id parent.(If there are multiple element with the same id).

So I was under the impression that $('#parent>p') would select only those p elements which are child of the first element found with id "parent".

But it seems to be matching all p element which are child of all the elements matched with id "parent".

I understand that id should be used to uniquely identify an element. Is it a bug or the expected behavior ??

Below is the code. <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>jQuery Selector</title> <script type="text/javascript" src="jquery-1.6.1.min.js">alert('hi');</script> </head> <body>

<h1>jQuery Selector Examples :</h1> <p> This paragraph has no parents.</p> <div id="parent">

<p> This paragraph is under a div whose id is "parent".</p> <span>

<p> This paragraph is under a span.</p>

</span>

</div> <div id="parent">

<p> This paragraph is under a div whose id is "parent2".</p>

</div> <div id="parent">

<p> This paragraph is under a div whose id is "parent2"(duplicate).</p>

</div>

</body> </html> <script type="text/javascript">

$(document).ready(function(){

/*CHILD: E>F*/ $('#parent').each(function(){ alert($(this).html()); }); $('#parent>p').each(function(){ alert($(this).html()); });

});

</script>

Change History (1)

comment:1 Changed 10 years ago by mikesherov

Resolution: notabug
Status: newclosed

Thanks for contributing!

Bad input == bad output. Simply don't use the same id for more than one element. We explicitly do not define expected behavior for invalid input.

Note: See TracTickets for help on using tickets.