Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to address element?

    Hi,

    I am trying to get the title from a hyperlink within a div that is defined to the code as a variable ($next) and I want to assign it to a variable called 'mytitle'. I have tried variations on this, but can't seem to get the syntax right. Can it be done?

    var $active = $('#slideshow DIV.active');
    var $next = $active.next().length ? $active.next() : $('#slideshow DIV:first');
    var mytitle = $($next "a").attr("title");



    <div id="slideshow">
    <div class="active">
    <div><a href="first.html"><img src="first.jpg" title="title1" border=0 ></a></div>
    <a href="second.html"><img src="second.jpg" title="title2" border=0 ></a></div>


    </div>

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    Is your html supposed to be:

    Code:
    <div id="slideshow">
    <div class="active"><a href="first.html"><img src="first.jpg" title="title1" border=0 ></a></div>
    <div><a href="second.html"><img src="second.jpg" title="title2" border=0 ></a></div>
    </div>
    In the code you posted you have an extra nested div. Also, the title is an attribute of the img tag not the anchor. Finally, your if statement on the length is incomplete. Assuming the html above is correct, try:

    Code:
    var $active = $('#slideshow .active');
    var $next = ($active.next().length > 0) ? $active.next() : $('#slideshow div:first');
    var mytitle = $next.find('img').attr("title");

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    553
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, SB65 - right on all counts! Thanks, again.

    G


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •