2

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.')

4 Answers 4

2

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.')
Sign up to request clarification or add additional context in comments.

Comments

1

You could do:

$("#Div").append( $( "<img>", {

    id: "myImage",

    src: "/images/myimage.png",

    click: function() {
        myfunction( 'addnew', $("#field").val() );
    }

}), " Error." );

4 Comments

I am trying to use this code on a span tag and the click event does not work . my code : $("#error").append($("<span>",{ id:"edit", title: action, click:function(){ Edit(action,$('#field').val() ); } }), errMsg);
@user829982 it's likely the Edit part that's not working, put click: function(){console.log("test");} to test if the click event is not working.
the console does not add test when i use the span tag only when i use the img tag. it does not seem that the edit function is not working
@user829982 did you realize that errMsg is not inside the span, but is a separate text node? So clicking that text is not same as clicking the span. Try $("<span>", {text: errMsg}) to make the text go inside the span..
1

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.')

Comments

0

Escape the single quotes around \'addnew\':

$("#Div").append('<img id ="myImage" src ="/images/myimage.png"  onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.')

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.