0

I am using JavaScript to generate some HTML tables on the fly. After filling out this dynamic table, I employ jQuery to 'grab' some of the inputs and do some small calculations for form validation. However, my jQuery selector does not work with the dynamic HTML form. Can anyone give me some suggestions? Thanks!

Here is the code:

<script>
//generate HTML table
$('<tr><th><label for="id_CAM_1">Input:</label></th><td><select name="CAM_1_'+i+'" id="id_1_'+i+'"><option value="1">Option 1</option><option value="2">Option 2</option></select></td></tr>').appendTo('.table');

// jQuery selector, which does not work dynamically

$('select[name=CAM_1_'+i+']').change(function() {
            var ss1=$(this).val()
            alert(ss1)})
</script>
3
  • 2
    Use event delegation by passing a selector to the on method. It's much more resilient than trying to manage binding and rebinding events. Commented Aug 28, 2012 at 16:40
  • 1
    Is your code actually missing the }) as above? otherwise it should work jsfiddle.net/mowglisanu/JKRGs Commented Aug 28, 2012 at 16:49
  • 1
    I see 2-3 questions on this almost daily. So many that I can't even bother to answer. :( Commented Aug 28, 2012 at 16:52

4 Answers 4

1

The following will detect all changes of all select elements that have a name that starts with CAM_1_ in your tableSelector

$('tableSelector').on('change', 'select[name^="CAM_1_"]', function() {
    alert(this.value);
});
Sign up to request clarification or add additional context in comments.

2 Comments

I guess the starts with selector was the real question then, eh? :)
You're plastering over the top of bugs elsewhere. The code originally posted works fine (when you add the closing brackets). It fails presumably because of some re-use of i in real code.
1

I wanted to replicate your problem, but for me it works completely fine. I did have to add the closing brackets (})) as these seemed to be missing from what you posted. Do you get any errors in the console. Check to see if you get any errors in the JavaScript console and whether you're using the latest version of jQuery.

<table class="table"></table>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
i = 0;
//generate HTML table
$('<tr><th><label for="id_CAM_1">Input:</label></th><td><select name="CAM_1_'+i+'" id="id_1_'+i+'"><option value="1">Option 1</option><option value="2">Option 2</option></select></td></tr>').appendTo('.table');

// jQuery selector, which does not work dynamically

$('select[name=CAM_1_'+i+']').change(function() {
    var ss1=$(this).val()
    alert(ss1)
});
</script>

3 Comments

I forgot to add the }) in the post. Thanks!
I mean is this sufficient to fix your problem? because Neal's answer doesn't make a whole heap of sense given the way you've asked the question. The code in the question now works fine.
It works now. I messed up 'i' in my real code. But I would like to switch to the 'on' approach.
1
<script type="text/javascript">
    $(function() {
        $('<tr><th><label for="id_CAM_1">Input:</label></th><td><select name="CAM_1_'+i+'" id="id_1_'+i+'"><option value="1">Option 1</option><option value="2">Option 2</option></select></td></tr>')
            .appendTo('.table')
            .on('change', function() {
                alert(this.value);
        });
    });
</script>​​​​​​​​​​​​​​​

Comments

0
<script>
//generate HTML table
$('<tr><th><label for="id_CAM_1">Input:</label></th><td><select name="CAM_1_'+i+'"   id="id_1_'+i+'"><option value="1">Option 1</option><option value="2">Option 2</option></select></td></tr>').appendTo('.table');

// jQuery selector, which does not work dynamically

$('select[name=CAM_1_'+i+']').live("change",function() {
        var ss1=$(this).val()
        alert(ss1)})
</script>

so basically you need to use live method of jquery for dynamically generated html.

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.