Say I have
var b = 'I am a JavaScript hacker.'
How can I do this ?
var b = 'I am a <a href="foo.php">JavaScript hacker</a>.'
Is this dooable ?
I thought the question was clear. Apologies if it wasnt.
Fiddle here http://jsfiddle.net/ozzy/vWYQ2/
asked Feb 2, 2012 at 5:05
422
5,77024 gold badges88 silver badges141 bronze badges
-
1What are you trying to achieve?grc– grc2012年02月02日 05:07:10 +00:00Commented Feb 2, 2012 at 5:07
-
Ok I have a text change script, that on hover changes text to another line of text .. and I wish to add a link within the result text I will create a fiddle422– 4222012年02月02日 05:08:26 +00:00Commented Feb 2, 2012 at 5:08
-
jsfiddle.net/ozzy/vWYQ2422– 4222012年02月02日 05:13:01 +00:00Commented Feb 2, 2012 at 5:13
-
What is criteria for selecting text from string which will become hyperlink text?Amar Palsapure– Amar Palsapure2012年02月02日 05:16:11 +00:00Commented Feb 2, 2012 at 5:16
-
@422 What about this jsfiddle.net/vWYQ2/1?Amar Palsapure– Amar Palsapure2012年02月02日 05:19:00 +00:00Commented Feb 2, 2012 at 5:19
3 Answers 3
$(function() {
var mem = $("#TA").html();
$("#TA").hover(function() {
$(this).stop().html( 'I am a <a href="foo.php">JavaScript hacker</a>.' );
}, function() {
$(this).stop().html( mem );
});
});
I think you want something like this
Edited: Due to the flickering issue.
answered Feb 2, 2012 at 5:24
jwchang
10.9k15 gold badges62 silver badges89 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
422
Thats great cept needs to have an id do i just go div.apple and change div id="apple" to initiate it ? because jsfiddle.net/ozzy/zKRGW/7 is flawed ( not sure what I done wrong there )
jwchang
@422 As you see I did not use an id. But you can use id and change it to $("#your_DOM_id") instead of $("div").
422
tried that and it flickers when you hover over it ? jsfiddle.net/ozzy/zKRGW/8
um... yes? that will give you a variable named b which holds 'I am a <a href="foo.php">JavaScript hacker</a>.'
answered Feb 2, 2012 at 5:07
Matt Briggs
42.4k17 gold badges103 silver badges128 bronze badges
Comments
Code from http://jsfiddle.net/vWYQ2/2/, this removes the hyperlink once mouse is out.
HTML
<div id="TA" onmousemove="changetext();" onmouseout="restore();">I am a JavaScript hacker.</div>
JavaScript
var originalBlock = document.getElementById("TA").innerHTML;
var timer;
function changetext()
{
var id = document.getElementById("TA");
if(originalBlock == null) originalBlock= id.innerHTML;
var text = id.innerHTML;
id.innerHTML = text.replace("JavaScript hacker", "<a href='foo.php'>JavaScript hacker</a>");
if(timer != null)
clearTimeout(timer);
}
function restore()
{
timer = setTimeout(function()
{
document.getElementById("TA").innerHTML = originalBlock;
}, 1000);
}
answered Feb 2, 2012 at 5:24
Amar Palsapure
9,6561 gold badge30 silver badges46 bronze badges
2 Comments
422
But how about just one word , this is where I am getting stuck I dont want "<a href='foo.php'>JavaScript hacker</a>" I am after "JavaScript <a href='foo.php'>hacker</a>" .This is where I am getting stuck ( escaping the different sections correctly
Amar Palsapure
@422 Try the new code it removes the hyper link. If you want to convert
hacker to hyperlink in that case change the replace statement to text.replace("hacker", "<a href='foo.php'>hacker</a>")lang-js