0

I have code like below but i am getting syntax error in 'onkeyup' function..

$('#file_lists').append('<div class="custom_text"><input type="text" onkeyup="this.value=this.value.replace(/[^1-9]/g,'');" name="custom_time" id="'+rands+'" value="1"></div>').after($("#final_result"));

please suggest me how to use this function.

3
  • Possible duplicate of JavaScript: SyntaxError missing ) after argument list Commented Mar 28, 2016 at 12:37
  • $('#file_lists').append('<div class="custom_text"><input type="text" onkeyup="this.value=this.value.replace(/[^1-9]/g,\'\');" name="custom_time" id="' + rands + '" value="1"></div>').after($("#final_result")); Commented Mar 28, 2016 at 12:37
  • Note the difference: you have '' in the middle of the string literal specified with single (') quotes. The parser thinks the string ends there and another begins. You need to escape those characters like Rayon did: \'. Commented Mar 28, 2016 at 12:54

1 Answer 1

4

Escape the single quotes in the replace function (.replace(/[^1-9]/g,\'\')) '' => \'\':

$('#file_lists')
 .append('<div class="custom_text"><input type="text" onkeyup="this.value=this.value.replace(/[^1-9]/g,\'\');" name="custom_time" id="'+rands+'" value="1"></div>')
 .after($("#final_result"));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.