Kaur
Kaur

Reputation: 501

Jquery Find first h1 on a page and append more html to its html

How can I find first h1 on a page and append more html to its html? I am trying something like this and it is giving me error -

 var str = $('h1:first').html();
    $('h1:first').html(str.append('some more html '));

Upvotes: 2

Views: 722

Answers (2)

The Process
The Process

Reputation: 5953

.html() can take the function as argument, and return from it current html with added new html.

$('h1:first').html(function(){
  return $(this).html() + ' some more html';
});

Upvotes: 2

aarcangelo
aarcangelo

Reputation: 58

What about

$('h1:first').html($('h1:first').html() + 'some more html');

You can put the string on variable like your code

var str = $('h1:first').html();
$('h1:first').html(str + 'some more html');

Upvotes: 0

Related Questions