Solved

JQuery - adding alert to find value of hidden field

Posted on 2011-03-01
4
676 Views
Last Modified: 2012-05-11
I am a complete newbie JQuery programmer (know some javascript), so please bear with me.  I have the attached code which pulls up a calendar and when the user chooses a date, writes the date into the $DAY$, $MONTH$, $YEAR$.  I don't know if the values are being written to these fields and I need to check this.  I looked at the source code and the values are empty but maybe you can't view the source of a hidden field?  How do I do an alert to check the values??
<script type="text/javascript">

$(document).ready(function() {

$('#Date').datepicker({
			onSelect: function(dateText, inst) {
				var s = dateText.split("/");
				$("#$DAY$").val(s[2]);
				$("#$MONTH$").val(s[1]);
				$("#$YEAR$").val(s[0]);
			}
		}); 
alert("day value is "+$DAY$.value);
$("#Date").datepicker('option', {dateFormat: 'dd/mm/yy'}); 
$('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel

});

</script>

<form method="post" action="http://www.exec.co.uk/external.asp">
<fieldset>
<input name="$LICENCE$" type="hidden" value="xxx" />
<input name="$SMOKING$" type="hidden" value="" />
<input name="$SOURCE$" type="hidden" value="" />
<input name="$LEDGER$" type="hidden" value="" />
<input name="$DAY$" type="hidden" value="" />
<input name="$MONTH$" type="hidden" value="" />
<input name="$YEAR$" type="hidden" value="" />
<label for="Date">Arrival <br/> Date</label>
<input id="Date" name="Date" type="text" value="22 Feb 2011" />
<label for="Nights">Number <br/> of nights</label>
<input id="Nights" name="$NIGHTS$" type="text" value="1" />
<input type="submit" name="Submit" value="Check for Availability" class="submit" />
</fieldset>
</form>

Open in new window

0
Comment
Question by:Lorna70
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:ChetOS82
ChetOS82 earned 47 total points
ID: 35007571
You are correct, you won't be able to see the new values of those hidden fields by viewing the source.  If you press F12 to bring up the developer tools you might be able to see the value.

Either way, your alert needs to be inside the onSelect function like this:
$('#Date').datepicker({
     onSelect: function(dateText, inst) {
          var s = dateText.split("/");
          $("#$DAY$").val(s[2]);
          $("#$MONTH$").val(s[1]);
          $("#$YEAR$").val(s[0]);
        alert("day value is "+$DAY$.value);
     }
}); 

Open in new window

0
 
LVL 18

Assisted Solution

by:ChetOS82
ChetOS82 earned 47 total points
ID: 35007642
I don't know Jquery, but I think your alert won't work if you simply use "$DAY$.value".  Perhaps you need to use something like alert("day value is" + $("#$DAY$")[0].value);

Also, if memory serves me, the $() function does a lookup again the "id" attribute, not the name attribute.  So you input boxes need id and name (they can be the same).  I could be wrong about this though.  
0
 
LVL 6

Accepted Solution

by:
mickey159 earned 180 total points
ID: 35024909
THe modified script should be like this:
I added id for the there hidden fields for data input.
Also I have corrected some of your mistakes such as the wrong position of the date and year,etc.
Also,I noticed that there is $ in the name.Is it asp markup?If no,please get rid of this or scripts may not work.

Good luck!
<script type="text/javascript">
$(document).ready(function() {

$('#Date').datepicker({
      onSelect: function(dateText, inst) {
        var s = dateText.split("/");
        $("#DAY").val(s[0]);
        $("#MONTH").val(s[1]);
        $("#YEAR").val(s[2]);
        alert("day value is "+$("#DAY").val()+"\nmonth value is "+$("#MONTH").val()+"\nyear value is "+$("#YEAR").val());
      }
    }); 
$("#Date").datepicker('option', {dateFormat: 'dd/mm/yy'}); 
$('a[rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel

});

</script>

<form method="post" action="http://www.exec.co.uk/external.asp">
<fieldset>
<input name="$LICENCE$" type="hidden" value="xxx" />
<input name="$SMOKING$" type="hidden" value="" />
<input name="$SOURCE$" type="hidden" value="" />
<input name="$LEDGER$" type="hidden" value="" />
<input name="$DAY$" id="DAY" type="hidden" value="" />
<input name="$MONTH$" id="MONTH" type="hidden" value="" />
<input name="$YEAR$" id="YEAR" type="hidden" value="" />
<label for="Date">Arrival <br/> Date</label>
<input id="Date" name="Date" type="text" value="22 Feb 2011" />
<label for="Nights">Number <br/> of nights</label>
<input id="Nights" name="$NIGHTS$" type="text" value="1" />
<input type="submit" name="Submit" value="Check for Availability" class="submit" />
</fieldset>
</form>

Open in new window

0
 
LVL 15

Assisted Solution

by:SRigney
SRigney earned 23 total points
ID: 35031168
The reason they are not being populated is that $("#$DAY$") is looking for a dom element with an id="$DAY$"  You don't have that in your code, which is why Mickey added id's for you.

If you want to not have the ID you could use
$('input[name*="$DAY$"]')

Here's a link to the attribute selector documentation.
http://api.jquery.com/attribute-contains-selector/
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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 how to dynamically set the form action using jQuery.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now