Vojislav Kovacevic
Vojislav Kovacevic

Reputation: 1535

Can't change text of a link with jquery

I have this code some link but I cannot change the text with jquery.

I tried

  if($("a").text() == 'some link') {
   $("a").text('success');
  }

but it doesn't work.

Any ideas?

Upvotes: 0

Views: 103

Answers (2)

Aakash Chakravarthy
Aakash Chakravarthy

Reputation: 10701

If you are talking about links, then you should get the "href" attribute using the attr property

$('a').each(function(){
  if($(this).attr('href')=="some link"){
      $(this).text("success");
  }
});

I hope this helps.

Upvotes: 0

Anton
Anton

Reputation: 32591

Do this

$('a').each(function(){
  if($(this).text()=="some link"){
      $(this).text("success");
  }
});

This will look through all a tags one by one and check if they have the "some link". With your code you are looking through all a tags and the text might be "link1link2link3link4"

Upvotes: 4

Related Questions