Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

JQuery - adding alert to find value of hidden field

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
Lorna70
Asked:
Lorna70
  • 2
4 Solutions
 
ChetOS82Commented:
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
 
ChetOS82Commented:
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
 
mickey159Commented:
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
 
SRigneyCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now