Zac
Zac

Reputation: 12836

Having html and variable in switch statement

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.

Upvotes: 2

Views: 143

Answers (3)

Mithun Sreedharan
Mithun Sreedharan

Reputation: 51282

try altering ' and "

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

Upvotes: 0

Guffa
Guffa

Reputation: 700562

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>

Upvotes: 0

jAndy
jAndy

Reputation: 236092

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/

Upvotes: 3

Related Questions