Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery text from span to title

Tags:

jquery

attr

I'm a jQuery newbie. I've something like the following HTML:

<div class=dcsns-youtube>
    <span class="section-thumb">
        <a href="some-link"><img src="http://img.youtube.com/vi/m4-1FZeoBtQ/default.jpg" alt=""></a>
    </span>
    <span class="section-title">
        <a href="some-link">Text on the link</a>
    </span>
<div>

I want to get the text that is inside the a element inside the span with the class "section-title", on the example "Text on the link" an use it as a title on the a element inside the span with the class "section-thumb".

The following jQuery code only works for one single div:

jQuery('.dcsns-youtube .section-thumb a').attr('title', $('.dcsns-youtube .section-title').text());

How can I make this work if I have several divs, adding to each a element inside span with the class "section-thumb" the respective on the text from the a element inside the span with the class "section-title"?

like image 237
user1970346 Avatar asked Feb 18 '26 08:02

user1970346


1 Answers

You could do this :

$('.dcsns-youtube').each(function(){
  $('.section-thumb a', this).attr('title', $('.section-title', this).text());
});

$(someselector, this) searches for elements inside this.

Demonstration

You might also pass a callback to attr :

$('.dcsns-youtube .section-thumb a').attr('title', function(){
      return $(this).closest('.dcsns-youtube').find('.section-title a').text();
});

Demonstration

like image 164
Denys Séguret Avatar answered Feb 19 '26 21:02

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!