views:

725

answers:

3

Hey guys, Say I had the following HTML:

<h2>Heading <span></span></h2>

<ul>
<li><a href="#" title="I am wanting to be in H2!">Something</a></li>
<li><a href="#" title="I too am wanting to be in H2!">Something else</a></li>
</ul>

I want to be able to take the title tag from the and place it inside the span of the h2. How can I do so?

Cheers.

A: 
$('h2 span').html($('title').html());

should work :)

i recommend giving the span an unique id, so to call $('#id').ht.... instead

henchman
It seems that the OP does not mean the `title` tag but the title **attribute** of an `a` tag.
Felix Kling
+1  A: 
$("a").each(function(){
  $("h2 span").append($(this).attr("title"));
});
Konrad Garus
Thanks Konrad, but I want to display the title in the span of the h2 when the a is clicked. I tried changing the 'each' to 'click' but still doesn't work. Any ideas?
lorenzium
@lorenzium: Please reflect this in your question.
svinto
+1  A: 
$('a').click(function(){
   $('h2 span').html( $(this).attr('title') );
   return false;
});
Paul Alexander
Isn't it interesting guesswork? :-)
Konrad Garus
Thanks Paul - I can get this to work if there is no other HTML in the page, but trying it in my actual page, it is not working... Maybe because it's conflicting with something else or I'm not targetting the classes properly. I don't know but thanks anyway.
lorenzium
Perhaps you have more h2s with spans?
Konrad Garus
Some error somewhere else in my code. But this works, so tick!
lorenzium