Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change input value [not reflected inside HTML]

Tags:

jquery

I am trying to change input value of a checkbox when it is checked/unchecked. It seems the value is changed (I debugged it via alert()) but is not reflected in the html. That's quite troublesome as I want to pull the value from the html and save it in a database.

                $(document).ready(function(){
                    $("#box:checked").live('click', function(e) {
                        $("#box").val('1');                          
                    });

                    $("#box:not(:checked)").live('click', function(e) {
                        $("#box").val('0');                            
                    });    
                 });

    ... 

      <input id="box" type="checkbox" value="">

    ...

I've been trying to solve this for hours. I came upon a guy who had the same problem, but the workaround was rather clumsy.

thanks for help

like image 229
dwelle Avatar asked Dec 05 '25 01:12

dwelle


2 Answers

You need to use attr for it to be reflected in the HTML.

$("#box").attr("checked", "checked");

http://jsfiddle.net/Xeon06/BwGeX/

like image 59
Alex Turpin Avatar answered Dec 07 '25 17:12

Alex Turpin


<input id="box" type="checkbox" value=""> - the "value" represents the load time html that the browser renders.

Runtime value of the Form elements are not updated in DOM inspectors

A checkbox's "Checked" value is not related to the "Value" attribute, though it can be manipulated to reflect a different value.

You can obtain the checkbox value as $("#box").is(':checked'), this returns true/false

like image 26
Ajaxe Avatar answered Dec 07 '25 18:12

Ajaxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!