0

Hi I'm trying to append javascript code when I click a button, but I think I messed up. If someone could help me with syntax. Maybe I'm doing it the wrong way; if someone has a better way to do it, it would be appreciated.

$("#btn_add").click(function(){   
 '<script>$(".selectcategory'+count+'").change(function() {var vv = ($(this).val());$( ".cat'+count+'" ).empty();$(".cat'+count+'").append("<input id="i_index" type="text" name="categorie[]" maxlength="255" style="width:80%" value="vv">");});</script>'
});
3
  • 1
    You don't have to use script tags. you can attach an event handler within another event handler Commented Jan 20, 2014 at 22:36
  • 1
    so many things wrong i dont even know where to start Commented Jan 20, 2014 at 22:37
  • @YuriyGalanter That way you bind an event handler many times. Commented Jan 20, 2014 at 22:41

1 Answer 1

2

Honostly there is a lot wrong in your code. I am learning Javascript myself at the moment, so maybe i won't provide the solution, but I am trying to help.

First of all, the script tag should be arround your code, not in it. Second use the single and double quotes correct. Last but not least, use some markup in your code. It's hard to see where the issue is.

<script>
$("#btn_add").click(function()
{   
    $(".selectcategory"+count).change(function()
    {
       var vv = ($(this).val());
       $(".cat"+count).empty();
       $(".cat"+count).append('<input id="i_index" type="text" name="categorie[]" maxlength="255" style="width:80%" value="vv">');
    });
});
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Greg, we were edditting the same thing, so I guess I am at least close ;)
ur right messing up with all those single quote and double quote now its working i forgot to put missing line on the code

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.