0

I'm trying to do some javascript work on this example:

var str= '<td><input id="product" type="checkbox" name="product' + i + '" value="Yes" />&nbsp; new product?</td>';
str = str+'<td><input type="text"class="form-control" id="number ' + i + '" name="number' + i + '" placeholder="GTIN"</td>'


<script>
 $(document).ready(function () {
    var ckbox = $('#product');

    $('input').on('click', function () {
        if (ckbox.is(':checked')) {
            document.getElementById("number").readOnly = true;
            document.getElementById("number").value = "";
        } else {
            document.getElementById("number").readOnly = false;            
        }
    });
});
</script>

This seems to work only if my input checkbox is outside the string builder

5
  • 1
    Your checkbox is in str. When are you adding that to DOM? Commented Nov 7, 2016 at 14:06
  • Where do you append the var str in the DOM? Commented Nov 7, 2016 at 14:06
  • Possible duplicate of Event binding on dynamically created elements? Commented Nov 7, 2016 at 14:06
  • See linked. Your $('input').on runs before you add it to the DOM Commented Nov 7, 2016 at 14:07
  • document.getElementById calls for an element with the id given, from inside the DOM, Commented Nov 7, 2016 at 14:07

1 Answer 1

2

If I understood your question correctly you are trying to check if input created as string is checked or not. To do so, you can try something like this:

HTML

<div id="number">
  <table id='mytable'>

  </table>

</div>

JS

var str;
for(var i = 0; i < 4; i++){
    str = '<td><input id="product'+i+'" type="checkbox" checked name="product' + i + '" value="Yes" />new product?';
  str = str+'<td><input type="text"class="form-control" id="number ' + i + '" name="number' + i + '" placeholder="GTIN" data-checkbox-id="product'+i+'"></td>'
    $("#mytable").append(str);
}

$("[id*='product']").on("click", function(){
    var this_id = $(this).attr("id");

    if( $(this).is(":checked") )
      $("input[data-checkbox-id='"+this_id+"']").removeAttr("disabled");
    else
      $("input[data-checkbox-id='"+this_id+"']").attr("disabled", "disabled");
 });

Check the Fiddle: https://jsfiddle.net/koky7zj1/11/
Is that what you are looking for?

Edit
See updated code. I'm glad I could help! :)

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

9 Comments

Hi Sebastian, what I'm trying to do is simply deactivate the textbox by clicking on the checkbox. It's not working since it's created like the example above
The reason i didnt post the whole code, it's because it might be to long, would u mind it?
Ok, I understand. Check the code in my previous comment and let me know if this is what you was looking for
Yes that's exactly what im looking for, but the input has to be in the (var str). Since it's a table that keeps adding data. So imagine for each line you will have to be able to do this function
Something like this? jsfiddle.net/koky7zj1/6 It isn't too hard to make it working for many elements
|

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.