Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery val function not working on a hidden field?

This is my HTMLcode:

<div style='display:none;' id='allformid'>
    <div>
        <form action='#'>
              <input type='text' name='name' id='named'/>
        </form>
    </div>
</div>

And this is my jQuery code to set the value of the input text box:

$("#allformid  #named").val('abcd');

This jQuery code is correct, but the form value is not changed.

like image 252
Renish Khunt Avatar asked Oct 29 '25 22:10

Renish Khunt


1 Answers

I tried the same code it works fine. How did you test that the code works or not?

If you check the inspect element, it will not show value="abcd". But if you make the div visible, you can see the value is given. But the value gets set. You can also test the value by getting the value in the js console like this:

$("#allformid #named").val();

However if you want it to display as value="abcd", you will need to write $("#allformid #named").attr("value",'abcd');

like image 192
Neville Nazerane Avatar answered Oct 31 '25 11:10

Neville Nazerane