JavaScript when is checked attribute of check box element added to DOM?


  I need to color all the check boxes that are checked to green and unchecked one's to red.

<style>
 input[type=checkbox]+span{
   color:red;
 }
 input[type=checkbox][checked]+span{
    color:green;
 }
</style>
<label>
    <input type="checkbox" class="bike"/>
    <span>I have a bike</span></label>
<label> 
    <input type="checkbox" checked class="car"/>
    <span>I have a car</span>
</label>

  It looks good when page is loaded. But later, when user clicks on any check box,thier color is not being changed.After debugging, I came to know that checked attribute is not added to input tag when user click happens. Can somebody tell me the reason?

I tried to put checked attribute explicitly then it worked well.

<button>Click</button>

    <script>
    $('button').click(function(){
        $('.bike').attr('checked',true);
    });
    </script>

But I dont want to use any script to achieve this. Is there any way to make it possible?
when actually the checked attribute is added to check box to make it checked? Here is the jsfiddle.

Answer:1

There's a :checked pseudo-selector (updated fiddle)

For completeness here's the current support for it: (from MDN)

Chrome  Firefox (Gecko)       Internet Explorer  Opera  Safari
1.0     1.0 (1.7 or earlier)  9.0                9.0    3.1
Answer:2

Please let me know if this is a duplicate post, but I was not able to find a solution to my issue. Its gotta be something simple... I'm trying to use javascript to build a form. The html for the form ...

Please let me know if this is a duplicate post, but I was not able to find a solution to my issue. Its gotta be something simple... I'm trying to use javascript to build a form. The html for the form ...

  1. line breaks from textarea
  2. line breaks form
  3. line breaks from word
  4. line break form html
  5. line break contact form 7
  6. line break pdf form
  7. line break permit form
  8. remove line break form
  9. line break online form
  10. jotform line break
  11. line break poetic form

I'm trying to send a an array to my sender from my receiver but when I try it it will turn it into a string. I have the code: messageBus = window.castReceiverManager.getCastMessageBus(namespace, ...

I'm trying to send a an array to my sender from my receiver but when I try it it will turn it into a string. I have the code: messageBus = window.castReceiverManager.getCastMessageBus(namespace, ...

  1. chromecast send message
  2. chromecast send message from receiver
  3. chromecast custom receiver send message

What is the difference between (null != someVariable) and (someVariable != null)? I have seen a lot of people using 'null' first in the comparison. Which one is better than the other and why?

What is the difference between (null != someVariable) and (someVariable != null)? I have seen a lot of people using 'null' first in the comparison. Which one is better than the other and why?

This is driving me nuts. I'm getting multiple mouseout events registering even though I'm getting rid of them with removeEventListener(). I've tried all kinds of variations of this. Essentially, once ...

This is driving me nuts. I'm getting multiple mouseout events registering even though I'm getting rid of them with removeEventListener(). I've tried all kinds of variations of this. Essentially, once ...