Javascript onclick function call problem: won't pass string

So the main point is that I am trying to create a rudimentary way to flag invalid content in our web mapping application. Inside a function that dynamically creates content for the sidebar of the webmap when the user clicks on a dot, I have this piece of code that should generate a flag image.

When the user clicks on the flag, I want to run the flagContent function, which should pass the url string to the function. From within this function, I could write it to the database later (although I haven't done it yet far).

Here are some of the code snippets I worked with:

1. It is there that the image of the flag is generated

content += "<p class='info'><img id='flag' onclick='flagContent(" + attachmentInfo.url + ")
'src='assets/flag.png' style='height:15px'>Flag as inappropriate...</p>";

      

  • This is a related function

    function flagContent (imageUrl) {console.log (imageUrl)}

So basically the url is a string and I would like to be able to manipulate it in the flagContent function. Unfortunately, I cannot get it to work. When I pass in a numeric parameter such as attachmentInfo.objectID I don't face the same problem.

For what it's worth, I also get this error:

Uncaught SyntaxError: Unexpected token:

Any help would be greatly appreciated. Let me know if there is any additional information that might help resolve this issue. Thank!

+3


source to share


1 answer


I am guessing this attachmentInfo.url

will return the url, which should be a string, and it should just be surrounded by quotes. Since you have already used both types of quotes, you will have to avoid some quotes.

content += "<p class='info'>";
content += "<img id='flag' onclick=\"flagContent('" + attachmentInfo.url + "')\" src='file.png'/>";
content += "Flag as inappropriate...";
content += "</p>";

      

Doing so makes the final conclusion like this:



<p class='info'>
  <img id="flag" onclick="flagContent('http://example.com')" src='file.png'/>
  Flag as inappropriate...
</p>

      

The problem was that the url was not surrounded by quotes and it saw flagContent(http://example.com)

and didn't know what to do with those bare words, not in a string.

+11


source







All Articles