JavaScript - How To Change A Textbox Value With Javascript Only When The Textbox Is Empty?
Hello. I've been teaching myself HTML and CSS for a while, and now I've moved into the world of Javascript (but I'm still very much a beginner). For practice, I've been building a sample sign up form for a mock website, and I'm having problems with the birthdate section. The idea I had was to have MM, DD, and YYYY be the default values of my 3 textboxes (as an example for users), then set the value to nothing when the box gained focus. That all works fine, but I ran into problems when I tried to write an if statement to set the value back to MM, DD, or YYYY if the value was still nothing when the user clicked away. As it is now it just replaces the text inputted into the textbox (which of course is not good). Any ideas for what the problem might be?
Code: <form name="signup" action="javascript:void(0);" method="post"> <table> <tr> <td>Date Of Birth:</td> <td> <input name="DOBmonth" type="text" value="MM" size="2" style="color: #555555;" onFocus="clearDOBmonth()" onBlur="restoreDOBmonth()" /> <input name="DOBday" type="text" value="DD" size="2" style="color: #555555;" onFocus="clearDOBday()" /> <input name="DOByear" type="text" value="YYYY" size="4" style="color: #555555;" onFocus="clearDOByear()" /></td> </tr> <tr> <td></td> <td><button name="Submit" type="submit" style="font-size: 1em;">Sign Up</button></td> </tr> </table> </form> <script type="text/javascript" language="javascript"> <!-- document.signup.DOBmonth.value="MM"; document.signup.DOBday.value="DD"; document.signup.DOByear.value="YYYY"; function clearDOBmonth() { document.signup.DOBmonth.value="";} function clearDOBday() { document.signup.DOBday.value="";} function clearDOByear() { document.signup.DOByear.value="";} function restoreDOBmonth() { if ('document.signup.DOBmonth.value = ""') { document.signup.DOBmonth.value = "MM"; // alert(document.signup.DOBmonth.value); } } //--> </script> Another side question, if I set a variable equal to the value of a textbox, then change the value of the variable, does that also change the value of the textbox? or is the variable no longer associated with the textbox. Example: Code: var a = document.form.textbox.value; a = blue; does document.form.textbox.value = blue? or is var a now completely independent of the textbox value? Thanks! Similar TutorialsHi there, I have this form that validates a few textboxes & a dropdownlist. And when it is not filled in, the border of the textboxes and dropdownlist would turn red, followed by an alert message notifying which field have not been filled in. Else, the border will revert back to black, and the form will be submitted successfully. Right now, I would like to prevent the user from entering blanks (like entering a space to cheat the system). How should I tweak the codes to make it work? Javascript Code: function check(checkForm) { var fields = new Array("Name","Email Address", "Domain Name"); var index = new Array(),k=0; for(var i=0;i<fields.length;i++) { var isFilled = false; var c = document.getElementsByName(fields[i]); for(var j = 0; j < c.length; j++) if(!c[j].value == "") { isFilled = true; c[j].className = "defaultColor"; } else { c[j].className ="changeToRed"; } if(!isFilled) { index[k++] = fields[i]; } } if(k>0) { joinComma = index.join(', '); alert('The field(s) corresponding to '+ joinComma + ' is/are not selected.'); return false; } } HTML Code: * Name: <input type="text" id="Text27" name="Name" /><br /> <br /> *Email Address: <input type="text" id="Text28" name="Email Address" /> @ <select id="Select5" name="Domain Name"> <option></option> <option>hotmail.com</option> <option>yahoo.com</option> </select> <input id="Submit5" type="submit" value="Submit" onclick="return check(checkForm)"/> Hi.. I have form and i want to input data using barcode and it will display on textbox and after input on the first textbox the focus will go to next textbox untill it will go on the last textbox and on the last textbox it will automatically save the data's on the database. How is it possible? here is my sample code: Code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); ?> <html> <head> <script type="text/javascript"> function ini() { // Retrieve the code var code =document.getElementById ("code_read_box1").value; var code =document.getElementById ("code_read_box2").value; var code =document.getElementById ("code_read_box3").value; var code =document.getElementById ("code_read_box4").value; var code =document.getElementById ("code_read_box5").value; var code =document.getElementById ("code_read_box6").value; // Return false to prevent the form to submit return false; } </script> </head> <body onLoad="document.barcode.code_read_box1.focus()"> <form name=barcode onsubmit = "return ini()"> <input type="text" id="code_read_box1" value="" /><br/> <input type="text" id="code_read_box2" value="" /><br/> <input type="text" id="code_read_box3" value="" /><br/> <input type="text" id="code_read_box4" value="" /><br/> <input type="text" id="code_read_box5" value="" /><br/> <input type="text" id="code_read_box6" value="" /><br/> </form> </body> </html> Hey guys, I Should have the textbox value as my radio button value. For example, if I type "5" in the textbox, then the radio button value would change to "5". by the way I have three radio buttons and one textbox and if I select one of those and type something in the text box the value would replace to radio button which is selected and all this have to happen before the page submitted or in the process of submitting. I hope I could find answers here (It's so important for me to solve this problem, For see the form please go to this page, Its above the news frame where you can see three radio buttons and one textbox named shipment tracking ...). thanks so much!!!
Could anyone show me a script that would allow the user whos on my site change the background image from the default color to an image from a URL? And then it would have to save their choice in a cookie. Thanks!
Hello, I need your help. Using Javascript, on mouseover/hover, I would like to change the boder color of my textbox from rgb(142, 142, 142) to black: rgb(0, 0, 0,) then when I move the mouse pointer off the textbox, I would like it to change from black back to rgb(142, 142, 142). I can't figure out where to start or how to get this going. A little help from the experts is needed here. Much thanks and appreciation for your time. Cheers, J Hey Guys, I have this really important question (I'm really stuck! I'm a baby coder ). I have four radio button and four textbox, each one of these textboxes has its own value which is provided from a database, now each one of these textboxes is related to a radio button and what happen is if I select radio button number one then the value of textbox number one is going to change by which is fine but when I'm going to select the other radio buttons (which is obvious that radio button number one is no longer selected) but textbox number one keeps the changed value and don't going back to its default value. let me give you my example (which I working on it right now), I have four textbox and four radio buttons, now my first textbox has a value="1" I select the first radio button and the first textbox value turned to value="2" now I select the second radio button and the second text value change from 1 to 2 just like the first text box (what happened is that the first radio button is deselected but the first textbox value didn’t turn back to its first value which was value="1" and keep the changed value which is value="2" (that happens to all text boxes and radio buttons as well) This is my stupid code: (please help me out here, I'm going to cry, Btw you can completely change the code if you want it’s not important I just need to restore these textbox to their first value if their related radio buttons is no longer selected. one more thing I manage to find a way to do that but I find out it is not stable (onblur) if I click somewhere else in the page (rather than the radio buttons) then the changed value return to default one which is SUCKS! I checked the other event handlers but didn’t work! <script type="text/javascript"> function firstaddit(){ if(document.getElementById("Rate1").value=="1") { document.getElementById("amount1").value="<%=(rs_vote.Fields.Item("Rate1").Value)+1%>" }} </script> <script type="text/javascript"> function secondaddit(){ if(document.getElementById("Rate2").value=="2") { document.getElementById("amount2").value="<%=(rs_vote.Fields.Item("Rate2").Value)+1%>" }} </script> <script type="text/javascript"> function thirdaddit(){ if(document.getElementById("Rate3").value=="3") { document.getElementById("amount3").value="<%=(rs_vote.Fields.Item("Rate3").Value)+1%>" }} </script> <script type="text/javascript"> function fourthaddit(){ if(document.getElementById("Rate4").value=="4") { document.getElementById("amount4").value="<%=(rs_vote.Fields.Item("Rate4").Value)+1%>" }} </script> <script type="text/javascript"> function ReturnFirstAddit(){ if(document.getElementById("Rate1").value=="1") { document.getElementById("amount1").value="<%=(rs_vote.Fields.Item("Rate1").Value)%>" }} </script> <script type="text/javascript"> function ReturnSecondAddit(){ if(document.getElementById("Rate2").value=="2") { document.getElementById("amount2").value="<%=(rs_vote.Fields.Item("Rate2").Value)%>" }} </script> <script type="text/javascript"> function ReturnThirdAddit(){ if(document.getElementById("Rate3").value=="3") { document.getElementById("amount3").value="<%=(rs_vote.Fields.Item("Rate3").Value)%>" }} </script> <script type="text/javascript"> function ReturnFourthAddit(){ if(document.getElementById("Rate4").value=="4") { document.getElementById("amount4").value="<%=(rs_vote.Fields.Item("Rate4").Value)%>" }} </script> Choice 1: <input type="radio" value="1" name="Rate" id="Rate1" onclick="firstaddit()" onblur="ReturnFirstAddit()"> <br/><br/> Choice 2: <input type="radio" value="2" name="Rate" id="Rate2" onclick="secondaddit()" onblur="ReturnSecondAddit()"> <br/><br/> Choice 3: <input type="radio" value="3" name="Rate" id="Rate3" onclick="thirdaddit()" onblur="ReturnThirdAddit()"> <br/><br/> Choice 4: <input type="radio" value="4" name="Rate" id="Rate4" onclick="fourthaddit()" onblur="ReturnFourthAddit()"> <br/><br/> <%=(rs_vote.Fields.Item("Choice1").Value)%> <input type="radio" value="1" name="Rate" id="Rate1" onclick="firstaddit()" onblur="ReturnFirstAddit()"> <br/><br/> <%=(rs_vote.Fields.Item("Choice2").Value)%> <input type="radio" value="2" name="Rate" id="Rate2" onclick="secondaddit()" onblur="ReturnSecondAddit()"> <br/><br/> <%=(rs_vote.Fields.Item("Choice3").Value)%> <input type="radio" value="3" name="Rate" id="Rate3" onclick="thirdaddit()" onblur="ReturnThirdAddit()"> <br/><br/> <%=(rs_vote.Fields.Item("Choice4").Value)%> <input type="radio" value="4" name="Rate" id="Rate4" onclick="fourthaddit()" onblur="ReturnFourthAddit()"> <br/><br/> Okay I have searched google for hours literally without luck so hopefully someone here can help. What I am trying to do is when someone submits the form if there are empty textboxes change the textbox's style from class="input" to class="inputreq". Someone please help, the formname is "signup" and the textboxes are "domain" and "username". Please help. Thanks. I have an asp.net textbox. I need to use javascript so that there are no postbacks. I would like to click the button and the textbox height kept increases each time the user clicks the button. So far I can toggle: function resize(){ var textbox = $get('<%=txtWhoTo.ClientID %>'); textbox.style.height = textbox.style.height == '56px' ? '23px' : '56px'; } I would like to increase the height 56px each time the clicks the button. hello, I have a textbox in my .net page, and totally new to java script. How do i get a script that will count the data being written in the text box to show increments of 100 and write the value to the bottom of the textbox. e.g. When u get to 100, at the bottom it says "1st hundred" ... and to 200 it writes "2nd hundred" UP to 500 ??? Please help thanks Hello I was at the javascriptkit.com and was to use one of there Javascript calenders in my web page the "Popup Date Picker" and the "Date Time Picker". I have the calenders working fine and after the date is selected it fills in the correct textbox with the date. The problem is after the user selects the date in the calender popup window I would like it to run another javascript function. But I cannot see in the calender code how to have it call my javascript code. I also tried the OnChange event with the Textbox that the Calenders are filling in with the date but it only fires when I click in the textbox. I call the Calenders JavaScript functions from my JavaScript script function that is tied to an aspropDownList OnChange Event. Is there any way I can have these calenders call a javascript function when the date is selected in there window? Or could I try to catch when the textbox is filled in with the date and call my function then? Thanks Mike I have a php script which dynamically creates a table containing text boxes with variable names. Example: Code: echo '<td align="center" bgcolor="'.$bgcolor.'"><input type="text" name="payable_'.$key.'" id="payable_'.$key.'" value="'.$payable.'" size="3" maxlength="4" /></td> '; In the above instance the text box name might be 'payable_23323'. I need to code an onKeyUp event which sends the text box name to a function, and in that function I need to retrieve the text box name and the string after 'payable_' together with the value the user has input into the text box. Any help is greatly appreciated . . . I have been Googling this for hours to no avail - probably because I don't know what to search for . . . Thanks Store Information to multiple textboxes from database Code: $count=<mysql_num_rows($query1); for($i=0;$i<mysql_num_rows($query1);$i++) { $row1=mysql_fetch_array($query1); $getval[$i]=$row1[UserId]; Print "<input type=text name='txtbox[]' id='txtbox[]' value='.$getval[$i].'">; } then i call a javascript function Print "<script language=javascript>Call_Fun($count)</script>"; The above code store data into textbox and call the js function in .js file Code: function Call_Fun(count) { for(var i=0;i<count;i++) { alert(document.getElementbyId('txtbox')+i.value); } } But it doesn't retreive values in .js Thankyou all Hi, I currently have a asp.NET page which has a textbox with the ID: "txtbox1" I would like javascript to store the data entered into txtbox1 into a var. I have tried using var jVarName; jVarName = '<%#aVarName%>'; but so far it hasnt worked. Any suggestions would be appreciated ^^ Hello, I'd like to write some javascript code which will check to see if the contents of a textbox are equal to some value after the user leaves the textbox. they should get a message saying Correct or Incorrect . As an example: Code: <td>-2</td> <td><input name="ans1" type="text" size="3" maxlength="3" /></td> </tr> < The correct answer here would be 5 > <tr> <td>-1</td> <td><input name="ans1" type="text" size="3" maxlength="3" /></td> < The correct answer here would be 4> And, I don't wish to abuse the forum, but as I'm learning javascript and jquery, I'd like to see solutions to both (I learn by example), so I'm posting the same question in the jquery forum with a request for a jquery solution; I hope that this isn't bad form! Thank you..... hi, i have a javascript code of autosugesstion.i had got it from internet. it works fine,but the problem whenever i type a word.. and then press enter it automatically takes the closest autosugessted word from the list ...for example 1.the list contains the word "an apple"..so when ever i type "app" in the textbox and then press enter ..it takes the word "an apple" from the autosugessted list . example 2..if i type "king"..and then press enter it automatically takes "kingdom".. actually i am looking for this solution : it should only take the word from autosugesstion list if selected from arrow key or mouse other wise it should take word from textbox which is typed by user hence can any one solve it ? kindly have a look at attachement. thanks in advance Hello folks, I have a content form. It has a textbox and a checkbox control (both are asp server controls and not HTML controls). When the user checks on the checkbox control, the textbox control has to become invisible. If unchecked the textbox control has to become visible. The following code works on a regular webform (meaning not as a content form). toggleVisibility is the function name that does the actual work. I am guessing I am not passing the textbox control ID properly to toggleVisibility function. Can someone guide me please? Following is the code: Code: <asp:TextBox ID="TextBoxPeriod" runat="server" Width="97px"></asp:TextBox> <asp:CheckBox ID="CheckBoxFromArchive" runat="server" Visible="true" Onclick="toggleVisibility('<%=TextBoxPeriod.ClientID %>');" /> function toggleVisibility(controlId) { var control = document.getElementById(controlId); if (control.style.visibility == "visible" || control.style.visibility == "") { control.style.visibility = "hidden"; } else { control.style.visibility = "visible"; } } Hello, Can you someone point me to the right script? I have a text box and would like to see if the value of the input matches the values defined in a JavaScript array. For example, the JavaScript array will hold five values: v1 = Tom v2 = Jerry v3 = Michael v4 = Stephanie v5 = Michelle If the text box does not equal one of those values, an alert will pop up stating it does not match. Thanks. i have an ajax/comet chat setup and working and i added the following line to it to play a sound when a new message is received... however every time it plays the sound, it unfocuses the textbox that the user is typing a new chat msg in.... Code: $('soundbox').innerHTML="<embed src='WAVE_358.wav' hidden=true autostart=true loop=false>"; hi this is santosh first off all thanks in advance to all who r reading my question im in prblm in my project in 1 file i have to first display textbox in html statically which will hav + & - button preceding it , & on click of + button one more textbox should appear dynamically & first textbox + button should disapper & it should hav only - button & the textbox which was dynamically generated now on click of first textbox + button should hav + & - button & it should go on. & on click of - button textbox which is in front of it should get deleted. here i tried alot but my code is showing + & - button to every textbox that is dynamically generated which is not the requirement plz help me im in great need. earlier also i hav submitted my queries & got a fully satisfied result bcoz of that now im having great hopes from u & this site plz reply as early as possible waiting for ur reply again thanks in advance Hi! I am quite new to the world of JavaScript as I have done basic corse for it... and I have learnt that best way to understand it is to play around with it... but now I am stuck with on of the problem!! Problem: How can we assign text box input values by user to a variable and display that variable back on another element of the same HTML document(dynamically)... Conditions: The value has to be stored as a JavaScript variable only. No server-side script is to be used and has to be done locally. Please help! |