Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox without checked attribute

I have been dynamically adding checkboxes to each row within a table - datatables.net . However, when I check the boxes, the html does not show any checked attribute which then does not allow me to only focus on the rows with checked checkboxes. If I set the checkbox with the checked attribute, then yes, the checked attribute is visible. The code here shows how I'm trying to check the checked attribute...

        var filtered_rows = example_data_table._('tr', {"filter":"applied"});

        $.each(filtered_rows, function(i, el){
            // If not checked, do not include
            if(!$(el[0]).is(':checked')){
                console.log(el[0]);
                return true;
            }else{
                window.alert("HIT");
            }
         ...

and the following is how I'm appending the element to the datatables.net table...

        var checkbox = $("<input type='checkbox' 
                           id='checkbox_" + o.example_id + "' />");

        ...

        tmp = [checkbox[0].outerHTML,
        ...];
        table_data.push(tmp);

        ...

        example_data_table.fnClearTable(false);
        example_data_table.fnAddData(table_data);

In the console, all I get is this message n times:

<input type="checkbox" id="checkbox_3895">                  examples.php:189


I guess, I'm wondering why the checked attribute is never included even when I have checked the checkbox?

like image 855
QuintoViento Avatar asked Sep 29 '14 19:09

QuintoViento


People also ask

What is checkbox value if not checked?

This might help: If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

Why clicking on checkbox does not add the attribute checked?

The DOM property checked is actually the current state of the checkbox and is either true/false. This will change when the checkbox is clicked, but isn't visible when you inspect the HTML. This is the answer.

How do you checkbox is checked or not ID?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Does checkbox have value attribute?

Note: Unlike other input controls, a checkbox's value is only included in the submitted data if the checkbox is currently checked . If it is, then the value of the checkbox's value attribute is reported as the input's value.


1 Answers

The HTML markup attribute <input checked="" is not actually designed to show the state of the element at runtime moment, but to define the default state of it when the page renders for the first time.

MDN < input > docs states:

checked:

When the value of the type attribute is radio or checkbox, the presence of this Boolean attribute indicates that the control is selected by default; otherwise it is ignored.

The only varying of checked state you can target is the Js DOM property checked:

document.getElementById('myInput').checked; //(true / false)

or the Css pseudo :checked:

#myInput:checked {
}
like image 174
LcSalazar Avatar answered Sep 16 '22 22:09

LcSalazar