Glen Gibb
asked on
How do I set a hidden input in jQuery?
Experts,
I need to set an hidden input's value to 0 in order to trigger an SQL insert statement. The button click
event and $("contact_id").val(0) executes, but the element value still remains 27.
I've tried various corrections: $("contact_id").attr('valu e', 0) or $("contact_id").prop('valu e', 0) but the id does not change!
What am I missing?
I need to set an hidden input's value to 0 in order to trigger an SQL insert statement. The button click
event and $("contact_id").val(0) executes, but the element value still remains 27.
I've tried various corrections: $("contact_id").attr('valu
What am I missing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Whoops. That's a typo. The hash is actually there in my code.
The code did read, $('#contact_id').val(0). But no value actually changed. I have used this expression successfully forever, but here the selector just doesn't work.
I have it working with $('input[name=contactId]') .val(0) which I found on a competing site.
My problem is code that doesn't work as advertised. I've spent untold hours on this. Why will the selector work with the name, but not with the id as expected?
The code did read, $('#contact_id').val(0). But no value actually changed. I have used this expression successfully forever, but here the selector just doesn't work.
I have it working with $('input[name=contactId]')
My problem is code that doesn't work as advertised. I've spent untold hours on this. Why will the selector work with the name, but not with the id as expected?
<input type="hidden" name="contactNameAttribute Value" id="contactIdValue" value="1234" />
to change value you MUST wait the total loading of the page using :
$(document).ready(function () {
$('#contactId').val( 0 ); // we use the Id attribute : <input type="hidden" id="contactId"
});
$('input[name=contactNameAttributeValue]').val("selected by name"); // you may have more than one field with the same ame attribute
$('#contactIdValue').val("selected by Id"); // Id attribute must be unique in a document
to change value you MUST wait the total loading of the page using :
$(document).ready(function
$('#contactId').val( 0 ); // we use the Id attribute : <input type="hidden" id="contactId"
});
ASKER
Page is already loaded when this block executes on "Add" button click. Id of 0 triggers an insert (else update.)
Appreciate the suggestion of the double id or name attribute. Searched both the code and the page source to verify there were no doubles of either name nor id.
The element is, <input type="hidden" class="id" id="contact_id" name="contactId">.
However, $("#contact_id").val(0); doesn't set the value. I think it ought to, but it still doesn't!
Appreciate the suggestion of the double id or name attribute. Searched both the code and the page source to verify there were no doubles of either name nor id.
The element is, <input type="hidden" class="id" id="contact_id" name="contactId">.
However, $("#contact_id").val(0); doesn't set the value. I think it ought to, but it still doesn't!
ASKER
Thanks, leakim. You're right as always! I tried to compose the selector (x + '_id').val.(0) but neglected the hash -- '#' + x + '_id.
Open in new window