pertrai1
pertrai1

Reputation: 4318

Setting hidden value from button click

I need to set the hidden input value when a button is clicked and it does not seem to be working.

<input type="hidden" name="addressOverride" id="addressOverride" value="">

Then the button

<button id="addressOverrideLink" class="global-button">Yes this is my correct delivery address</button>

And the code that is trying to set it

if ($('#addressOverrideLink'))
    {
        $('#addressOverrideLink').click(function(e){
            $('#addressOverride').val('YES');
            $('#mainForm').submit();
            return false;
        });
    }

Upvotes: 2

Views: 9660

Answers (3)

Hussein
Hussein

Reputation: 42808

You can use plain JavaScript to do it

document.getElementById('addressOverride').value = "YES";

Or if you want to use jQuery, do it like this

$('input[name=addressOverride]').val('YES');

Check working example at http://jsfiddle.net/xM5E7/

Upvotes: 2

mattsven
mattsven

Reputation: 23253

That...should work. Only think I can suggest is to do this instead:

if($('#addressOverrideLink').length > 0){
/* other stuff here */
}

Upvotes: 0

alex
alex

Reputation: 490153

You should check the length property of the jQuery object in your condition.

Upvotes: 0

Related Questions