?
Solved

JQuery - adding alert to find value of hidden field

Posted on 2011-03-01
4
Medium Priority
?
694 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
[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
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:ChetOS82
ChetOS82 earned 188 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 188 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 720 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 92 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

Industry Leaders: 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!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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

762 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