2

What would be the correct way to accomplish this ?

var parameter = json.Parameter;
switch (foo)
  { 
      case "0" : foo = "There is no link";
        break;
      case "1" : foo = "Here is a link : <a href=\"www.alink.com/?" + parameter + "\">Link B</a>";
        break;
  }
$("#result").append( foo);

The way I tried this b would just look print "Here is a link : " but not the actual link.

4
  • Can you tell us what "links" stands for? And what do try to accomplish? Commented Jun 28, 2011 at 8:00
  • It is just an arbitrary variable name... a foo. In the actual scripts I am working with it is an integer that is returned in an xml.. I changed my question so hopefully it is less confusing. Commented Jun 28, 2011 at 8:03
  • 1
    I don't like to edit people's code to tidy it up... but you might want to slap a few line indents in there ;-) Commented Jun 28, 2011 at 8:03
  • Seems to work for me: jsfiddle.net/HjP2G (I did add the http protocol to the url though) Commented Jun 28, 2011 at 8:04

3 Answers 3

3

You can invoke the pretty unknown method .link() like:

links = "Here is a link: " + "Link B".link("http://www.google.com");

$("#result").append( links );

That is cross-browser since Netscape navigator days. However, I guess your actuall problem was that you didn't quote the value of the href attribute. But, you can just use .link() to be fine here.

Example: http://jsfiddle.net/SNHeW/

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

2 Comments

ahh.. sadly the problem was in my real code, a forgotten quote! Doh.. so I should go to sleep.. but.. almost.. done...
@Sushi K: That is why you should show the actual code that you are using. It's hard to find a problem in the code you are using by looking at some other code that you are not using...
0

try altering ' and "

foo = "Here is a link : <a href='http://www.alink.com/?" + parameter + "'>Link B</a>"

2 Comments

You get the answer .. because you were the closest.. I just screwed up the quotes in my real code.
any way you have to add the http protocol part
0

When I try it, it adds the link just fine:

http://jsfiddle.net/RsMKP/

You need a http:// in the URL for it to work, though.

Javascript:

var foo = '1';
var parameter = 'asdf';
switch (foo) {
  case "0" :
    foo = "There is no link";
    break;
  case "1" :
    foo = "Here is a link : <a href=\"http://www.alink.com/?" + parameter + "\">Link B</a>";
    break;
}
$("#result").append(foo);

HTML:

<div id="result"></div>

Comments

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.