jQuery - Set Form's attribute to true on page load

1k Views Asked by At

Hope someone can help me on this as I cannot figure out what I'm doing wrong. I have checked different posts but I cannot find anything different of what I'm already trying.

My model has an attribute: private boolean thisShouldBeTrue;

The value of this should dynamically change depending on user's actions on the screen. My form contains a hidden input to retrieve this value:

<form:input path="thisShouldBeTrue" type="hidden" id="retrievedValue" />

Different javascript functions show this working since I retrieved different values on submission when the user makes some action to trigger those scripts.

However, I cannot set this to true on page load (this is the value that should be sent by default if user take no other action).

$(document).ready(function(){
        $('#retrievedValue').val("true");
});

Any suggestions?

2

There are 2 best solutions below

4
Bhushan Kawadkar On

you just need to use # for selecting html element with id using jquery, see below

$(document).ready(function(){
   $('#retrievedValue').val("true");
});
3
Burak Ayyildiz On

JQuery uses the Xpath semantics to identify elements in the DOM. To access an element through an ID, in this case retrievedValue, you can access it by $("#<id>").

$(document).ready(function(){
            $('#retrievedValue').val("true");
    });