?
Solved

How do I set a hidden input in jQuery?

Posted on 2014-10-21
6
Medium Priority
?
210 Views
Last Modified: 2014-10-22
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('value', 0) or $("contact_id").prop('value', 0) but the id does not change!

What am I missing?
0
Comment
Question by:Glen Gibb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40396137
you need to add the hash to select the hidden input by it's ID attribute :

$("#contact_id").val(0);

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40396139
more info here :
http://api.jquery.com/id-selector/

and here for the others selectors :
http://api.jquery.com/category/selectors/
0
 

Author Comment

by:Glen Gibb
ID: 40397443
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?
0
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 
LVL 82

Expert Comment

by:leakim971
ID: 40397482
<input type="hidden" name="contactNameAttributeValue" id="contactIdValue" value="1234" />

$('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

Open in new window


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"
});
0
 

Author Comment

by:Glen Gibb
ID: 40397856
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!
0
 

Author Comment

by:Glen Gibb
ID: 40397880
Thanks, leakim.  You're right as always!  I tried to compose the selector (x + '_id').val.(0) but neglected the hash -- '#' + x + '_id.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question