0

The object on the page

<DIV style="TEXT-INDENT: 0px; VISIBILITY: inherit" id=button46 jQuery16107163179349561973="2">
    <A title=Exit href="javascript:void(null)" name=button46anc>
        <IMG style="CURSOR: pointer" border=0 name=button46Img alt=Exit src="images/btn-exitoff.gif" width=66 height=39>
    </A>
</DIV>

I need to get the value of the title attribute from the div.

Put a few hours into this with no luck (all return undefined)

$('div [id^="button"]').bind('click mouseover mouseout submit',function(event){
    testThis = $(this.children('a'));alert($(testThis).attr('title'));
    testThis2 = $(this.find('a'));alert($(testThis2).attr('title'));
});

Thanks in advance for saving th rest of my hair.

3 Answers 3

3
$('div[id^="button"]').bind('click mouseover mouseout submit', function(event) {
    testThis = $(this).children('a');
    alert(testThis.attr('title'));
});
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Since you also got rid of the incorrect descendant selector.
@patrick dw seen you around SO. Love you. I consider it a high compliment. Thanx.
looks like I can use children or find() if I need to get a specific element type - one faster/better than the other?? Thanks so much for the help.
@Peter From the jquery docs: The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree. Children would be faster in this case.
0

The contents of this is not a jQuery object, it's a DOM element. Wrap it in a jQuery object to use the children and find methods, and then you don't need to wrap the result:

$('div [id^="button"]').bind('click mouseover mouseout submit',function(event){
  var testThis = $(this).children('a'); alert(testThis.attr('title'));
  var testThis2 = $(this).find('a'); alert(testThis2.attr('title'));
});

Also, declare the variable locally, so that they don't end up in the global namespace.

Another variation is to use the DOM element as context for a search:

var testThis = $('a', this);

2 Comments

Thanks. Understand the need to declare locally - this was just to test so I didn't go local with "var". Can't do a global on the a elements as I ONLY need those whose div id begins with 'button'.
@Peter: The last variation doesn't do a global search, it only searches inside the element that this refers to. See the context parameter: api.jquery.com/jQuery
0

You need to turn this into a jQuery object before you can use jQuery functions on it.

$('div [id^="button"]').bind('click mouseover mouseout submit',function(event){
    alert($(this).find('a')).attr('title'));
});

2 Comments

Thanks to ALL for the help - have to do this more instead of beating my brain to death.....
Since you're new here, I'll explain that you should vote up the answers that helped you and click on the checkmark by whichever one best answers your question. That both encourages and rewards people for helping (they get points) and helps sort out which answers provided better information for those who might read this Q&A sometime later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.