0

When trying to pass String value to the javascript function , Uncaught ReferenceError is thrown on the browser console. Below is the sample code:

function mySampleTest(myId, comments){
    alert("myId " + myId);
    alert("comments : " + comments);
}

var myTest = function(value, rowIndex) {
    var myId = this.grid.getItem(rowIndex).MY_ID;
     var comments = this.grid.getItem(rowIndex).COMMENTS;
    return "<img src=<%=request.getContextPath()%>/images/image1.gif width=\"25\" height=\"25\" onClick=\"mySampleTest("+ myId +" , "+comments+")\">";
};

The JavaScript function mySampleTest is being called when the user clicks the image but it throws a JavaScript error when I pass the string comments to mySampleTest function. If I remove the comment parameters and just pass the myId to mySampleTest(..), it works fine. Please suggest how to pass string values to the JavaScript function. I tried the below also, but didn't work.

 return "<img src=<%=request.getContextPath()%>/images/image1.gif width=\"25\" height=\"25\" onClick=\"mySampleTest("+ myId +" , \'' + comments + '\')\">";
9
  • 1
    what is the exact error? What does browser source version look like? If this is in a js file none of your server code will be compiled. Commented Apr 10, 2015 at 18:34
  • 1
    Does the error tell you a specific line or part of the code that causes it? Commented Apr 10, 2015 at 18:35
  • How does your browser render your img html snippet? Commented Apr 10, 2015 at 18:38
  • 3
    can you try this? return "<img src=<%=request.getContextPath()%>/images/image1.gif width=\"25\" height=\"25\" onClick=\"mySampleTest('"+ myId +"' , '" + comments + "')\">"; Commented Apr 10, 2015 at 18:46
  • 1
    thanks @JDB. I believe his html is not getting rendered properly and hence the error. if he can just add a single quote before and end of the variable then it should work. Commented Apr 10, 2015 at 18:52

1 Answer 1

3

As a professor once told me, much of writing code involves "being the computer".

Consider your function's output for a moment and you should see the issue pretty quickly:

<img src=whatever/your/context/path/is/images/image1.gif
     width="25" height="25"
     onClick="mySampleTest(12345, this is a comment)">

Your javascript is invalid:

mySampleTest(12345, this is a comment)

It should be:

mySampleTest(12345, 'this is a comment') // <--- notice the quotes

Which would translate all the way back to:

return "<img src=\"<%=request.getContextPath()%>/images/image1.gif\" width=\"25\" height=\"25\" onClick=\"mySampleTest('"+ myId +"' , '"+comments+"')\">";

Not to mention your src attribute really needs quotes.

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

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.