Reputation: 121
I am trying to pass two arguments in the on click javascript function. I need to enclose the the arguments in quotes. when i enclose it in single quotes it breaks the quote that starts from image and when i use double it breaks the quotes in front of javascript. how do i enclose it that it doesn't break my code?
$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction('addnew',$("#field").val())"/> Error.')
Upvotes: 2
Views: 1566
Reputation: 140230
You could do:
$("#Div").append( $( "<img>", {
id: "myImage",
src: "/images/myimage.png",
click: function() {
myfunction( 'addnew', $("#field").val() );
}
}), " Error." );
Upvotes: 1
Reputation: 7761
Escape the single quotes around \'addnew\'
:
$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.')
Upvotes: 0
Reputation: 3248
You will have to escape the quotes with a backslash, as such:
$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.')
Upvotes: 2
Reputation: 56429
Try this, you need to escape the apostrophe with \
:
$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.')
Upvotes: 1