Solved

JQuery - adding alert to find value of hidden field

Posted on 2011-03-01
4
681 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make Session back to empty 9 30
Head to not include on scroll 4 23
Insert Button on a table 16 37
javascript - insert into js doc help 2 23
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
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)

856 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