0

I'm creating a javascript function that builds a table with checkboxes. I want associate a click event to them. The event must be the same for all.

This is a part of my code:

var i = 0;
for (i = 0; i < data.Items.length; i++) {
    var tr = $(document.createElement('tr'));
    tr.addClass("MyBookings_table_content");
    $("#MyBookings_table").append(tr);

    //Create a CheckBox Element
    var chkbox = document.createElement('input');
    chkbox.type = "checkbox";
    chkbox.id = "chk"+i.toString();
    chkbox.name = "chk" + i.toString();
    //Event here
}

Can anybody help me?

5 Answers 5

2

Since you are using jQuery, you can simply add this event listener :

$("#MyBookings_table").on("click", "input[type=checkbox]", clickCB);

function clickCB() {
    var $cbx = $(this);

    // some code on my checkbox
}

This code is to be appended after the loop, see this fiddle : http://jsfiddle.net/6f79pd5y/

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

4 Comments

but If I do an alert window inside the click function to show the value of the combo box, a lot of windows open! And I just want one windows, because I only click on 1 checkbox.
Yes this code is to append after the loop (and not inside the loop)... I have added a fiddle if you want to see how to use it...
Another thing if you can help me again. It's possible in click function add all checked checkboxes to an array ? Thank you
Yes sure : use $("#MyBookings_table").find("input:checked"). But this is a jQuery object with all checked boxes. If you want an array with all DOM elements corresponding to the checked boxes, use instead : $("#MyBookings_table").find("input:checked").toArray()
0

create a function and link it with an addEventListener

function outerFunction(){
}
checkbox.addEventListener('click', outerFunction);

Comments

0
addEventListener("click",myFunction);  

where you have the comments

2 Comments

It's working! thank you. Is possible pass to "myFunction" the combobox object? something like addEventListener("click",myFunction(this)); ?
yeah,you have to do it a bit different though.addEventListener("click",function(e){ myFunction(this); }) Try this and let me know.
0

How about:

var i = 0;
for (i = 0; i < data.Items.length; i++) {
    var tr = $(document.createElement('tr'));
    tr.addClass("MyBookings_table_content");
    $("#MyBookings_table").append(tr);

    //Create a CheckBox Element
    var chkbox = document.createElement('input');
    chkbox.type = "checkbox";
    chkbox.id = "chk"+i.toString();
    chkbox.name = "chk" + i.toString();

    //Event here
    $(chkbox).click(function()
                    {
                       alert("Click!"); 
                    });

    tr.append(chkbox);
}

A working example:http://jsfiddle.net/mpup12z5/

Comments

0

If you reorganise your element-creation to use jQuery also, you can bind the click-handler at the point of creation:

var i = 0;
for (i = 0; i < data.Items.length; i++) {
    var tr = $(document.createElement('tr'));
    tr.addClass("MyBookings_table_content");
    $("#MyBookings_table").append(tr);

    $('<input />', {
        'type' : 'checkbox',
        'id' : chk + i, // i will be converted to a string automatically
        'name' : chk + i,
        'click' : functionToCall // note: no parentheses, you want to
    });                          // call the function, not use its return value
}

Or:

var i = 0;
for (i = 0; i < data.Items.length; i++) {
    var tr = $(document.createElement('tr'));
    tr.addClass("MyBookings_table_content");
    $("#MyBookings_table").append(tr);

    $('<input />', {
        'type' : 'checkbox',
        'id' : chk + i, // i will be converted to a string automatically
        'name' : chk + i,
        'on' : {
            'click' : functionToCall
        }                            
    });
}

But, it really is preferable to delegate the event-handling to the closest ancestor element, presumably the <table>, unless a specific property of the created <input /> should call a different function, or do something somewhat different.

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.