Solved

checkbox change event not running

Posted on 2013-05-15
11
274 Views
Last Modified: 2013-05-16
I have a form being built dynamically.  In the process I am adding the class 'check1' and 'check2' to certain check boxes.

I have the following code to test the 'change' event of the checkbox but neither alert is showing.  I try it in jsfiddle and I see it works fine.

I tried taking it out of the document.ready function but perhaps I got the syntax wrong.  

Currently, in document.ready I have:
$(document).ready( function(){
        $('.check1').change(function() {
            alert('clicked');
            $('#outputArea').text(this.checked ? "is checked" : "is not checked");
        });
        
        $("input[type=checkbox]").change(function() {
                alert('clicked');
        });

Open in new window

0
Comment
Question by:KCTechNet
  • 6
  • 5
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39169153
What do you mean dynamically? With ajax..?
0
 

Author Comment

by:KCTechNet
ID: 39169226
yes
0
 
LVL 58

Expert Comment

by:Gary
ID: 39169246
$(document).ready( function(){
        $('.check1').on("change",function() {
            alert('clicked');
            $('#outputArea').text(this.checked ? "is checked" : "is not checked");
        });
        
        $("input[type=checkbox]").on("change",function() {
                alert('clicked');
        }); 
})

Open in new window

0
 

Author Comment

by:KCTechNet
ID: 39169249
You can find all my code in another open question:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28126558.html

I changed line 94 to:
strTD+= "<td class='Side" + intColumnLoop + "'> <input type='checkbox' class='check" + intColumnLoop + "'";

Open in new window


and added the alerts shown in my original post.
0
 

Author Comment

by:KCTechNet
ID: 39169262
your last post didn't work either.  I have a feeling it has something to do with line 94 where I 'build' the checkboxes, but I don't see it.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Expert Comment

by:Gary
ID: 39169292
Have you got a live page to look at, or your fiddle.
0
 

Author Comment

by:KCTechNet
ID: 39169639
This is on our Intranet.  Not sure how to use fiddle without the ASP data
0
 

Author Comment

by:KCTechNet
ID: 39169787
Ok.  I think the issue is where I am placing stuff:
$(document).ready( function(){
        createTabs();
        
        $('#outputArea').text($('body').html());
        $('.check1').on("change",function() {
            alert('clicked');
            $('#outputArea').text(this.checked ? "is checked" : "is not checked");
        });

Open in new window


in the '#outputArea', I get the hml as it looks BEFORE the call to the createTabs() function.  The function is on top of the document.ready.  Should all my functions be inside the document.ready section?  Or is there a way to 'refresh' the html because I am guessing the '.check1'.change is not finding any classes because it is referencing  HTML that is not 'drawn' yet???
0
 
LVL 58

Expert Comment

by:Gary
ID: 39169809
That is why I changed your code to use .on which will bind to any dynamic content.

Is check1 a tab?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39169816
Ahh I forgot, you need to bind to an existing element
Change container to an element that exists to start with

$('container').on('change','.check1',function() {
0
 

Author Comment

by:KCTechNet
ID: 39169919
looks like I gotta read up on binding.  Not sure what you did and why but it worked. :)

Yes, checkboxes are within tabs.   I combined your code with existing tab stuff:
        $("#tabs" )
            .tabs({
                beforeActivate: function(event,ui){
                    //  if (startCheck){return (saveForm(1)==1);}       //uncomment this line if you want to save at each tab click
                },
                select: function( event, ui ) {
                    currTab=$(ui.tab).attr('href');
                    var n = currTab.lastIndexOf("#");
                    currTab = currTab.substring(n+1);
                }
            })
            .on('change','.check1', function() {
                alert('clicked');
                $('#outputArea').text(this.checked ? "is checked" : "is not checked");
            });  

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Populating html form in Sharepoint using JSOM 11 49
jquery wizard next doesn't go to top of tab 4 51
Example unit tests with AngularJS 3 40
What is assert.deepEqual? 4 39
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now