Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JQuery - adding alert to find value of hidden field

Posted on 2011-03-01
4
Medium Priority
?
696 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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

618 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