Nested ID in JQuery selector
jquery descendant selector
jquery selector with parent id
jquery parent > child selector
jquery get id
jquery greater than selector
jquery id selector
I am trying to change the href of an a nested in a div called blink3:
Where the html code is as follows:
<div style="float:left;text-align:center;" id="blink3"> <a href="#item4" class="panel" id="backLink"> <div class="divBack"></div></a> </div>
works well in all browsers - NOT IE 7 and I cannot figure out why, doesn't change the href.
Is this a IE 7 bug? JQuery? anything wrong with my code?
Nick Craver (omg, where have you been around?) already stated it in a comment. It totally makes no sense to create a selector like
#id #id because an ID has to be unique within your DOM by definition.
It looks like you break that rule and therefore, jQuery might select a wrong node.
You should replace your "backlink"
classes and go for the selector like
I don't see any duplication of ID's in the question and I too had the same question even though all of my ID's are unique. A perfectly valid reason for wanting to do nested ID selectors is to retrieve an element if and only if it is a child of a specific parent. I've run into this with drag and drop UIs as well as tree nodes. While not a selector, this is what I've used in the past:
Child Selector (“parent > child”), Child Selector (“parent > child”) child selector. Description: parent: Any valid selector. child: A selector to filter the child elements. <li>Nested item 1</li>. The #id Selector. The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element. To find an element with a specific id, write a hash character, followed by the id of the HTML element:
IDs are supposed to be unique within a page. As Nick implies, you should have only one ID with the value of
backlink. So a selector of
$('#backlink') should be enough by itself. If you have more than one ID with the value
backlink, then your HTML is wrong and you should fix that instead.
To search nested elements using the
id is as simple as concatenate id selectors, as for example:
In this case we are searching a modal with id
selector_modal and inside of it a h4 element that has in this case the id
How do you target a nested element inside of a class?, To select #nav a , wrap it in a jQuery object and Bob's yer uncle. $('#nav a'). method(). #nav a is a CSS selector for any anchor contained in the element by id� Selects all sibling elements that follow after the “prev” element, have the same parent, and match the filtering “siblings” selector. Also in: Selectors > Basic Filter :not() Selector
Multiple Class / ID and Class Selectors, Select the element which has an ID of header and also a class name of callout. Maybe this graphic will make that more clear: Combinations of Classes and IDs. The elements will be filtered by testing whether they match this selector; all parts of the selector must lie inside of an element on which .find() is called. The expressions allowed include selectors like > p which will find all the paragraphs that are children of the elements in the jQuery object. Consider a page with a basic nested list on it:
ID selectors, The following ID selector matches the H1 element whose ID attribute has the the fictional start tags of the first-line pseudo-elements were nested just inside the � jQuery Forum Move this topic Forum : Getting Started Using jQuery Using jQuery Plugins Using jQuery UI Developing jQuery Core Developing jQuery Plugins Developing jQuery UI QUnit and Testing About the jQuery Forum jQuery Conferences jQuery Mobile Developing jQuery Mobile
- How many
id="backLink"do you have in your code?
- IDs are supposed to be unique to a page and if you have a number of the same IDs inline then I would imagine this is the root cause of your problems. Classes, on the other hand, are meant to be assigned to a number of different DOM elements.
- Youre quick....I know I shouldn't have more than one... but I need it for some other things. would this cause the problem, and only in IE 7?
- OK. So i need you experts to tell me : can I write $(this).attr('class')=="backlink"? as oppose to $(this).attr('id')?
- @NinaNa: Probably yes. Duplicate
ids do cause problems in IE7 where they usually don't cause problems in newer browsers.
- Out of curiosity. What if NinaNa has got multiple
id="blink3"and he/she wants to access multiple ones of them? If
$('blink3')is saved in a variable
blink3.find('#backLink')be faster than