-1

How do I change the code syntax below, where is says infoAddress from a number into a string value? What syntax am I missing.

var html  = '<a onclick="selectMarker(' + i + '); selectAddress(' + infoAddress + ');">';
8
  • 2
    What are i and infoAddress? Commented Sep 30, 2013 at 13:53
  • Do not build HTML in JavaScript. What are you really trying to do? Commented Sep 30, 2013 at 13:54
  • @minitech why is building HTML in JavaScript bad? Commented Sep 30, 2013 at 13:55
  • and what do you mean a number to a string value? You are already outputting its value with what you are doing. You can mean make a 5 output as five can you? :O Commented Sep 30, 2013 at 13:57
  • @user2782160: Because it is usually to put into innerHTML, which is almost always wrong. Use the DOM. Commented Sep 30, 2013 at 14:04

1 Answer 1

3

If I get your question right, you're trying to give the parameter for the selectAddress function as a string. For the parameter must be in quotes. As you're already in a string, you've got to escape the single quotes with a backslash.

var html  = '<a onclick="selectMarker(' + i + '); selectAddress(\'' + infoAddress + '\');">linkText</a>';
Sign up to request clarification or add additional context in comments.

8 Comments

Maybe I'm not understanding the question. Here's a little fiddle with what I think the OP is trying to achieve: jsfiddle.net/EsmgT. If that's not it, I'm happy to delete my answer again.
@Michael, you have not just misunderstood the question, but you appear to have a misunderstanding of javascript syntax... what you have written is simply invalid code. (Edit: and your JSFiddle doesn't even produce a link)
@freefaller: Maybe you didn’t understand where he meant to put it? var html = '<a onclick="selectMarker(' + i + '); selectAddress(\'' + infoAddress + '\');">';
@minitech, you're correct - my apologies Michael... it wasn't at all obvious, and I'm having a bad day... I retract my objections (although the fiddle still doesnt' work)
Thank you. But I still can't really tell if this is actually what the OP asked for.
|

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.