Here is the code I have:
$('#link').attr("href",link)
$('#link').text(text)
How do I change the title of a link using jQuery? I'm correctly changing the url, but I can't edit the text, what am I doing wrong?
<a id="link" href="" target="_blank">text</a>
$('#link').attr("href",data[1].url)
$('#link').attr("title",data[1].title)
<a href="">title</a>
I'm tring to simply change 2 things:
- url
- title (as show above)
I'm able to change the link, but the title won't change. I'm selecting the wrong trhing. Therefor is there a way to list all attr available to me? Or are you able to help me change the text title above?
Either answer is acceptable.
<div id="highlight" class="topicHighlight hero1">
<h3 id="h3">hero_1_large_text</h3>
<p id="p"></p>
<span id="coverTextSpan">hero_1_small_text</span>
<a id="link" href="url" target="_blank">text</a>
</div>
Here is the code I have:
$('#link').attr("href",link)
$('#link').text(text)
How do I change the title of a link using jQuery? I'm correctly changing the url, but I can't edit the text, what am I doing wrong?
<a id="link" href="" target="_blank">text</a>
$('#link').attr("href",data[1].url)
$('#link').attr("title",data[1].title)
<a href="http://link.com">title</a>
I'm tring to simply change 2 things:
- url
- title (as show above)
I'm able to change the link, but the title won't change. I'm selecting the wrong trhing. Therefor is there a way to list all attr available to me? Or are you able to help me change the text title above?
Either answer is acceptable.
<div id="highlight" class="topicHighlight hero1">
<h3 id="h3">hero_1_large_text</h3>
<p id="p"></p>
<span id="coverTextSpan">hero_1_small_text</span>
<a id="link" href="url" target="_blank">text</a>
</div>
Share
Improve this question
edited Aug 29, 2011 at 6:43
JZ.
asked Aug 29, 2011 at 5:54
JZ.JZ.
21.9k33 gold badges119 silver badges193 bronze badges
4
- maybe u'd better write a ; after ur code – zhzhzhh Commented Aug 29, 2011 at 5:58
- 1 @zhzhzhh: Semicolon statement terminators are actually optional in JavaScript. – mu is too short Commented Aug 29, 2011 at 5:59
- @mu is too short: I know,but did u find what's the reason cause that problem?I don't find that yet. – zhzhzhh Commented Aug 29, 2011 at 6:04
- related: Change Link text with CSS – joeljpa Commented Mar 25, 2024 at 15:11
4 Answers
Reset to default 11use html function:
$('#link').html(text);
or , if you are talking about the title attribute:
$('#link').attr('title','some title');
you can try this javascript only.
document.getElementById('link').innerHTML = "new title";
i think this will surly helpful to you..
Thanks.
I think you need to use the HTML() method to change the content of the anchor tag. Here is the link to the documentation.
If all these answers don't work for you then you should check:
- What element are you selecting and see if it's the correct one
- What variable are you assigning to the title(data[1].title)
I recommend using firebug in firefox or using the dev console in google chrome, anduse the console.log() function to log things so that you don't have to alert() them all the time.