1

I have script that append input to the page and the input with onclick function. This function get 3 parameters. But i get SyntaxError: expected expression, got end of script.

my code look like this:

scntDiv.append('<input id="coupon_id'+currentItem+'" type="button" value="Create Coupon" onClick="randomString(coupon_id'+currentItem+',ttt'+currentItem+',lll'+currentItem+');">');

Any Hellp?

7
  • How does the onclick handler part look like? Commented Aug 18, 2015 at 12:21
  • argument inside randomString should be enclosed with quotation, I believe. Commented Aug 18, 2015 at 12:27
  • But if i close them i get error. Commented Aug 18, 2015 at 12:28
  • Post the whole code please Commented Aug 18, 2015 at 12:31
  • if i open firebug i see: onclick="randomString(coupon_id2,ttt2,lll2);" Instead of: onclick="randomString('coupon_id2','ttt2','lll2');" Commented Aug 18, 2015 at 12:34

2 Answers 2

1

Your syntax was wrong at the end, here's the right version:

scntDiv.append('<input id="coupon_id'+currentItem+'" type="button" value="Create Coupon" onClick="randomString(\'coupon_id'+currentItem+'\',\'ttt'+currentItem+'\',\'lll'+currentItem+'\')">');

If you want to use ' you'll have to escape the character by using \ like: \'

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

1 Comment

Thank you very much , Just what I needed.
0

Though inline handlers work, it would be ideal to do something like this:

document.getElementById('coupon_id' + currentItem).onclick = function () {
    //your code here
};

Or, if you are using jQuery, just

$('coupon_id' + currentItem).on('click', function () { /* ... */ });

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.