Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-10-2011, 04:26 PM   PM User | #1
gorilla1
Regular Coder

 
Join Date: Jun 2002
Posts: 544
Thanks: 0
Thanked 0 Times in 0 Posts
gorilla1 is an unknown quantity at this point
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>
gorilla1 is offline   Reply With Quote
Old 05-10-2011, 05:38 PM   PM User | #2
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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");
SB65 is offline   Reply With Quote
Old 05-10-2011, 09:02 PM   PM User | #3
gorilla1
Regular Coder

 
Join Date: Jun 2002
Posts: 544
Thanks: 0
Thanked 0 Times in 0 Posts
gorilla1 is an unknown quantity at this point
Thanks, SB65 - right on all counts! Thanks, again.

G
gorilla1 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:24 AM.


Advertisement
Log in to turn off these ads.