3

I'm working with google maps api and I want the label of a marker have a link that execute a warning.

var str = "hi";
var mp_position = createGoogleMapsLocation(data[i].location);
var text = "<p>Edit: <a href=# onClick='edit("+str+")'>Click here</a></p>";
addMeetingMarker(mp_position, text);

The addMeetingMarker function works, but edit function not. The code is:

function edit(message) {
    alert(message);
}

If you set an integer in the argument of the edit function it works, but passing the variable str does not work. Why?

3 Answers 3

6

You need more double quotes to surround your value string - so:

var text = "<p>Edit: <a href=# onClick='edit("+str+")'>Click here</a></p>";

becomes

var text = "<p>Edit: <a href=# onClick='edit(\""+str+"\")'>Click here</a></p>";
Sign up to request clarification or add additional context in comments.

Comments

1
edit("+str+")

will result in the javascript code

edit(hi)

Where hi is an unknown variable. Depending on what you want the edit function to do, either add a quote: edit\""+str+"\"), or remove the plusses: edit(str). In the last case, the edit function will receive the str variable, in the first case, it will receive the "hi" literal.

Comments

0

You're missing the double quotes necessary to make str a quoted string.

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.