2

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/

5
  • 1
    What are you trying to achieve? Commented 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 fiddle Commented Feb 2, 2012 at 5:08
  • jsfiddle.net/ozzy/vWYQ2 Commented Feb 2, 2012 at 5:13
  • What is criteria for selecting text from string which will become hyperlink text? Commented Feb 2, 2012 at 5:16
  • @422 What about this jsfiddle.net/vWYQ2/1? Commented Feb 2, 2012 at 5:19

3 Answers 3

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

My code

Edited: Due to the flickering issue.

Sign up to request clarification or add additional context in comments.

3 Comments

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 )
@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").
tried that and it flickers when you hover over it ? jsfiddle.net/ozzy/zKRGW/8
2

um... yes? that will give you a variable named b which holds 'I am a <a href="foo.php">JavaScript hacker</a>.'

Comments

2

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);
}

2 Comments

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
@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>")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.