So knowing all the common languages and never really having any need for JavaScript, I'm having issues with ASP PostBack.
I started using JavaScript to do things on the page so there's NO postback (thereby easily preserving any listbox selections, et. al.) which is more efficient and faster for the user, it's really starting to grow on me. Trouble is, and I should "Get A Book" but I'd rather learn as I go ... so ... I need to click a radio button from a list and depending on what is selected, fill a TextBox with the relevant information.
SO ... I have a schedule form. The shifts are set times, either 8 hours or 12 hours and the shift can start on one day and cascade into the next; pretty simple. Instead of having to type the date and time into the START and END boxes (tedious when scheduling 20-30 things at a time), I want to script a radio button list to autofill the text boxes for me.
So, here's what I have ...
In the <HEAD> section, inside the <script type="text/javascript" language="javascript"> block, I have...
Now of course I'll expand this to calculate my dates ... but I can't even get it working on the most BASIC level ... as in...Code:function fcnSetMyStartEndDatesPlease() { var radioButtons = document.getElementsByName("rblMyShiftType"); for (var x = 0; x < radioButtons.length; x++) { if (radioButtons[x].checked) { alert("You checked " + radioButtons[x].id); } } }
In the ASPX page, I have...
THIS WORKS. The alert pops up the value I selected, awesome.HTML Code:<input id="Radio1" type="radio" name="rblMyShiftType" value="1" onclick="fcnSetMyStartEndDatesPlease()"/> <input id="Radio2" type="radio" name="rblMyShiftType" value="2" onclick="fcnSetMyStartEndDatesPlease()"/> <input id="Radio3" type="radio" name="rblMyShiftType" value="3" onclick="fcnSetMyStartEndDatesPlease()"/>
NOW ... when I add this (the 3 lines of red code below where I will start the logic to put the dates in), the alert pops up with the value selected, then the page hangs up and I have to force close the browser... (and NO I don't want it to say "hello" ... but it won't even load the box with a single word!) ... if I can just get this to work, I can do the rest, there's some stupid thing missing I suppose...
NOW ... I back it down to next to nothing ... and make a button that does this ...Code:function fcnSetMyStartEndDatesPlease() { var radioButtons = document.getElementsByName("rblMyShiftType"); for (var x = 0; x < radioButtons.length; x++) { if (radioButtons[x].checked) { alert("You checked " + radioButtons[x].id); } if (x == 1) { var tbxMyStartDate = document.getElementById("tbxStartDate"); tbxMyStartDate.value = "hello"; } } } }
Call this with a button, AND THIS WORKS ... it puts "hello" in the box.Code:function fcnThisIsFrustrating(){ var tbxMyStartDate = document.getElementById("tbxStartDate"); tbxMyStartDate.value = "hello"; }
WHAT THE HECK! I copy/paste the two lines of the fcnThisIsFrustrating code into the the fcnSetMyStartEndDatesPlease (inside the IF/THEN) and it completely hoses the browser on IE9, IE10, IE11 and FireFox 42.0, which are the only browsers I need for now to work.
The only answer is there's a problem in the IF/THEN operator ... but HOW?! You can't have an IF/THEN in a FOR loop in JavaScript? Then WHY THE HECK does the ALERT BOX WORK ... BUT NOT THE tbxMyStartDate.value assignment?
Thanks in advance.



Reply With Quote
Bookmarks