Stuart
Stuart

Reputation: 11

Use checkbox to set val() for field

Total N00b here, I have a long form wizard that needs one step to be dynamically shown/hidden dependant on a radio button. All is cool with the code thus far.

function checkRb () {
            if($(this).is(":checked")){
            //yes
            $("#do_frick").val("step5");
            //alert ("yeah");
            }else {
            //no
            $("#do_frick").val("submit_step");
            //alert ("no");
            }
            }

The issue is that the hidden field "#do_frick" is several steps further down the page. If I place it in the same div as the checkbox, values change no problem. Place "#do_frick" further down the form and it doesnt change.

I expect that $(this) is only looking inside the current div and not the entire document. I got this far with the help of a very good programmer and dont want to annoy him further with N00b questions, any help greatly appreciated :)

It appears that if I put the "#do_frick" inside a div that is further down the page the form wizard sets the to display:none and anything inside the div cannot be set... would this be correct ?

Upvotes: 1

Views: 235

Answers (3)

Stuart
Stuart

Reputation: 11

SO after much noodle scratching the answer is a little bug/hiccup in jQuery

Whenever the field is in a div that has the rule "dsiaply: none;" applied jQuery cant reach inside and change it. Checking with firebug, the field has an additional value "disabled=''". Using the command

$('#do_frick').get(0).disabled = false

Sorts it out.. finally:)

Final code:

function checkRb () {
            if($(this).is(":checked")){
            //yes
            $('#do_frick').get(0).disabled = false //The important bit
            $("#do_frick").val("step5");
            //alert ("yeah");
            }else {
            //no
            $("#do_frick").val("submit_step");
            //alert ("no");
            }
            }

Upvotes: 0

Nrj
Nrj

Reputation: 6831

look for the id in generated html as suggested above.

Also look for any duplicate id shared by any other element, this will mess up things.

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

Please view source of your page to check what is ID of hidden field when it is several steps down. If ID is same then position should not be a problem.

this is not current div. In event handlers it means the object that raised the event. You should check jquery.com for more information on this.

PS:- try setting value of hidden field like this

 $("#do_frick").attr("value","submit_step");

Upvotes: 1

Related Questions