views:

53

answers:

4

In jquery how do I refer to a div with a certain title attribute?

$('#inner').attr("title:contains('" + position +"')").css('position', 'absolute');

this isnt working for me, If contains doesnt work in there how do I refer to a div with the title="div1" and change its css . Thanks again!

EDIT: I have multiple #inner divs, how can I go about referring to a specific one, if title will not work? There must be some way to give these #inner divs some sort of identification so I can call them individually.

+2  A: 

You want to use .attr('title') to get the value of the title attribute.

.attr isn't for selecting elements, you can't pass the :contains selector to it.

meagar
A: 

To select those with title attributes:

$("#inner > *[title!=]").css('position','absolute');
thephpdeveloper
+6  A: 

If you're looking for a div that is an ancestor of #inner and has the title div1, do this:

$('#inner').find('div[title=div1]').css('position','absolute');

EDIT:

Something tells me that you my have more than one <div id='inner'> and you're trying to get the one with a particular title.

If that is the case, your markup is invalid. You can not have more than one element on a page with the same ID.

You'll have to rethink your strategy if that's the case.


Here's an example of using a class attribute instead of ID, and selecting based on the title attribute:

HTML

<div class='inner' title='div1'>some content</div>

jQuery

$('.inner[title=div1]').css('position','absolute');

This will select the element with the class inner that has the title div1. It uses the "attribute equals" selector.

There are many other types of selectors in jQuery that you can use. Here's a reference:

patrick dw
+1  A: 

rename your divs so they have a unique id and use class="inner" then use .each to cycle though them or use similar code as above $('.inner').find('div[title=div1]').css('position','absolute'); jquery doesnt like it when you dont play by the rules (multiple element with the same "unique" id

Traveling_Monk