• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

504
Views
Why clicking on checkbox does not add the attribute checked='checked'

When I click a checkbox, why does checked attribute is not getting added?. You can see the code here http://jsfiddle.net/FCrSg/

over 3 years ago · Santiago Trujillo
3 answers
Answer question

0

The HTML attribute checked means: checked by default, when the page loads. This won't change when the checkbox is clicked.

<input type="checkbox" checked="checked"> <!-- The HTML attribute -->

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.

$('input:check')[0].checked == true;
// Whether or not the checkbox is currently checked
over 3 years ago · Santiago Trujillo Report

0

What are you trying to do? Find out if its checked?

$('.user_roles').click(function(){ 
    console.log( $(this).is(':checked'));
});

http://jsfiddle.net/petersendidit/FCrSg/1/

over 3 years ago · Santiago Trujillo Report

0

If you want to see it appear on the element displayed in the console, use the native setAttribute() method.

Example: http://jsfiddle.net/FCrSg/2/

this.setAttribute('checked',this.checked);

So it would look like this:

$('.user_roles').click(function(){
    this.setAttribute('checked',this.checked);
    console.log( $(this) );
});

Then the console should give you:

<input class=​"user_roles" type=​"checkbox" checked=​"true">​

Though you normally wouldn't need the attribute set like that. Typically the property is enough.

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error