Skip to main content

Bug Tracker

Side navigation

#13608 closed bug (notabug)

Opened March 14, 2013 10:04AM UTC

Closed March 14, 2013 02:32PM UTC

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>

Attachments (0)
Change History (1)

Changed March 14, 2013 02:32PM UTC by mikesherov comment:1

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.