JavaScript - Moving The Curser From Text Box To Text Box Automatically When A Condition Is Met
Hello there, this is my first post on this forum.
I have learned a good bit of PHP and have implemented it for use in my work as a math tutor. My kids are telling me, however, that it is too inconvenient to hit tab or click on the next text box and would prefer to use the old style pencil and paper. I thought of a good idea: Javascript would be able to automatically focus the curser on the next text box if some condition was met. For example: 6+7. If the textbox reads 13, it will focus on the next text box. Otherwise, nothing happens. This kills three birds with one stone; the user will know if they got the question right or wrong and it will move automatically if they got it correct. The problem is I lack any real JavaScript wisdom. I would guess this would be quite simple. The closest thing I have found upon searching was this from the user requestcode, but this has to do with once the user has typed in 4 characters it moves automatically. Code: <SCRIPT LANGUAGE="JavaScript"> function nextbox(fldobj,nbox) { if(fldobj.value.length>3) {document.forms[0].elements[nbox].focus()} } </SCRIPT> </head> <body onLoad="document.myform.txt1.focus()"> <CENTER> <FORM NAME="myform"> <INPUT TYPE="text" NAME="txt1" SIZE="4" MAXLENGTH="4" onKeyUp="nextbox(this,1)"> Thank you for the help. Similar TutorialsI already looked at this http://codingforums.com/showthread.p...ht=moving+text thread and I know it's similar. I just don't know what I am doing wrong. My css puts the text where it needs to go, then the script is supposed to alter it's pixel position and so it should move, right? Code: <html> <head> <style> item1 {position: absolute; left:20px; top:280px;} item2 {position: absolute; right:20px; top:280px;} </style> </head> <body> <item1 id="item1" >item1</item1> <item2 id="item2" >item2</item2> <script type="text/javascript"> var i=0; var j=0; var obj1=document.getElementById(item1); var obj2=document.getElementById(item2); obj1.style.visibility="visible"; obj2.style.visibility="visible"; //if (typeof obj1.style.left!="undefined" && obj2.style.right!="undefined") //{ //loop from ParseInt to coordinates in center of screen for (i=0;i<100;i++) { obj1.style.left=parseInt(obj.style.left) + i + "px"; document.write("<item1 id="item1">item1</item1>"); document.close(); } //for (j=0;j<100;j++) // { // obj2.style.right=parseInt(obj2.style.right) + j + "px"; // document.write("<item2 id="item2" >item2</item2>"); // } alert(i); //} </script> </body> </html> Am I anywhere close? I am trying this on IE6 (I only have Windows 2000) and on Firefox 3.5.3. Hi. Stuck on an assignment, inserting 3 files that i am working with, unable to get the unhide or move functions working. Thanks for all your help. ***Index.htm Code: 01.<html> 02.<head> 03.<!-- 04. New Perspectives on JavaScript 05. Tutorial 4 06. Review Assignment 07. 08. Avalon Books 09. Author: Rexdreamer 10. Date: 08/12/09 11. 12. Filename: index.htm 13. Supporting files: ab.gif, ablogo.gif, avalon2.htm 14.--> 15.<title>Avalon Books</title> 16.<script type="text/javascript" > //insert embedded script that uses an if statement to test for W3C DOM 17. if (document.getElementById) location.href="avalon2.htm" //if supported browser load avalon2.htm using the href prop of the location obj 18.</script> 19. 20.</head> 21. 22.<body style="background-color: black"> 23.<p style="text-align: center"> 24.<img src="ablogo.jpg" alt="Avalon Books" /><br /> 25.<img src="ab.gif" alt="" /> 26.</p> 27.</body> 28.</html> ***Avalon.htm Code: <html> <head> <!-- New Perspectives on JavaScript Tutorial 4 Review Assignment Avalon Books Author: Chris Veal Date: 08/12/09 Filename: avalon2.htm Supporting files: kids.jpg, fiction.jpg, nfiction.jpg, scripts2.js, styles2.css --> <title>Avalon Books</title> <link href="styles2.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="script2.js"></script> <script type="text/javascript"> function placeObjects() { //insert function named placeObj var w = winWidth/2; //declare variable named w that is =1/2 of the value returned by the winWidth function placeit("avalon2", W-75, 0); placeit("kids", W-75, 100); //place kids coordiantes at w75,100 placeit("fiction", W-75, 100); placeit("nfiction", W-75, 100); setZ("kids",z3) //use setZ functionto set zindex for kids object to 3 setZ("fiction",z2) //use setZ functionto set zindex for fiction object to 2 setZ("nfiction",z1) //use setZ functionto set zindex for nfiction object to 1 moveAvalon(); //call the moveAvalon function } function moveAvalon() { var x = xc if (x <= w-125); shiftIt("avalon",10); setTimeout("moveAvalon",50); } if (x > w-125) { } } function swapImages() { //insert swapimages function, purpose to change the stacking order of the 3image obj setTimeout("showIt('kids')"); setTimeout("showIt('fiction')"); setTimeout("showIt('nfiction')"); setTimeout("swapIt()", 2000); } </script> </head> <body onload="placeObjects()"> <div id="kids"> <img src="kids.jpg" alt="" /><br /> Sale this month on children's books </div> <div id="fiction"> <img src="fiction.jpg" alt="" /><br /> Fiction selection of the month </div> <div id="nfiction"> <img src="nfiction.jpg" alt="" /><br /> Non-fiction selection of the month </div> <div id="avalon"> Avalon <span id="books">Books</span> </div> </body> </html> ***script.js Code: /* New Perspectives on JavaScript Tutorial 4 Review Assignment Avalon Books Name: Chris Veal Date: 08/12/09 Function List: xCoord(id) Returns the x-coordinate of the object with id with the value id placeIt(id, x, y) Places the id object at the coordinates (x,y) shiftIt(id, dx, dy) Moves the id object dx pixels to the right and dy pixels down showIt(id) Hides the id object by setting its visibility style to "visible" winWidth() Returns the width of the interior browser window in pixels winHeight() Returns the height of the interior browser window in pixels setZ(id, z) Sets the z-index value of the id object swapIt(id1, id2, id3) Swaps the z-index values of id1, id2, id3 */ function setZ(id, z) { // create new function called setZ with 2 parameters object=document.getElementById(id); // use the value of the id parameter to select the obj in the document with that id value object.style.zIndex=z; // set the z index of the object to the z parameter } function swapIt(id1, id2, id3) { // create new function called swapIt with 3 parameters var object1=document.getElementById(id1); //getObj function to create the 3 variables named obj 1-3 var object2=document.getElementById(id2); var object3=document.getElementById(id3); var z1=object1.style.zIndex; //create 2 variables named z1-z3 set variables=to zIndex values of obj1-3 var z2=object2.style.zIndex; var z3=object3.style.zIndex; object1.style.zIndex=z3; //change the zIndex value of obj1 to z3thus moving it to the bottom of the stack object2.style.zIndex=z1; //change the zIndex value of obj2 to z1thus moving it to the top of the stack object3.style.zIndex=z2; //change the zIndex value of obj3 to z2thus moving it to the middle of the stack } function placeIt(id, x, y) { var object=document.getElementById(id); object.style.left=x+"px"; object.style.top=y+"px"; } function shiftIt(id, dx, dy) { var object=document.getElementById(id); object.style.left=parseInt(object.style.left)+dx+"px"; object.style.top=parseInt(object.style.top)+dy+"px"; } function xCoord(id) { object=document.getElementById(id); xc=parseInt(object.style.left); return xc; } function showIt(id) { var object=document.getElementById(id); object.style.visibility="visible"; } function winWidth() { if (window.innerWidth) return window.innerWidth; else if (document.documentElement) return document.documentElement.offsetWidth; else if (document.body.clientWidth) return document.body.clientWidth; } function winHeight() { if (window.innerHeight) return window.innerHeight; else if (document.documentElement) return document.documentElement.offsetHeight; else if (document.body.clientHeight) return document.body.clientHeight; } If anyone has a PS3 and goes on the PSN Store (PlayStation Network Store) you will get what i'm on about but does anyone know how I can use Javascript to make it when a user hovers over some text it will marquee but start from the position it was? Example: A string like this wouldn't need to have a scroller when someone hovers over it: Hello but a string like this would: Hello i'm bob and how are you doing today?. as it would be to long to stay on one line in the div. So what i'm looking for is some sort of marquee that doesn't start off screen but calls the current texts position and then starts from there and then reverse itself to go from right-to-left to left-to-right when it's at the end of the text and when the user's mouse moves off the text it will just go back to its normal state. All text's which are to big for the div will have three dots added to the end and once mouse over they will go and the full text will start to scroll. If anyone can help me this would be a massive help. Thank You DJCMBear I work at a college radio station and our brand new website just went live, but we're having one particular issue... We have a set list of shows that air each day, and on the site we have a marquee tag on the homepage that scrolls through showing each show and its broadcast time. Right now we're stuck changing this schedule each day by hand, but it's tough considering we're all college students ourselves and sometimes our own schedules simply don't allow the time. Is there a code that would allow this kind of a daily update to happen automatically? The format for the schedule is such: 4 - 5 pm // "Show 01" 5 - 6 pm // "Show 02" 6 - 8 pm // "Show 03" 8 - 10 pm // "Show 04" 10 - 12 am // "Show 05" Any help would be very much appreciated! Hi all, I'm trying to get the cursor position to move to the end of the text within an editable iframe or div. I've been pointed to something called TextRange and have been reading up on it for days yet couldnt make sense of much (i'm still very new). what i'm trying to do is on a button click, the cursor will move to the end of the text in the iframe. any help or pointers would be greatly appreciated. thanks. As of right now I have a code that will work in IE but wont work in FireFox...go figure. Basically what I want to have happen is when you type in an area code it will provide an output in a predetermined area of the page. For Example: Input- 512 Output - Austin, TX The code that I have doesn't work with firefox and I was just wondering if there was a code that would allow that to happen. Thanks! Hi, I have a form with 9 text fields and a text area. What I want to do is replace the text in the text area depending on how many fields contain text. For example my text boxes are named 1 to 9, if the user enters text in the first five boxes I want the text area to auto fill with 'you have selected boxes 1 to 5' if the user selects all nine it will say 'you have selected 1 to 9', therefore, the user must complete the text boxes in order. I have it working with an onchange event but i have a button on the form to also auto fill the text boxes and it does not work if this is clicked. Any help would be appreciated! Hi all, Thanks for reading. I'm having an issue trying to accomplish the following - I have a text field (field1) already displayed on the HTML page. However, there's a link where you can add additional text fields to the page as well. When the link is clicked, the second text field is added successfully (field2), and when the link is clicked again, the third text field (field3) is added successfully. However, the third field does not add itself to the page, and the text for anything greater than a third field also isn't displayed after. This obviously means that my "fields" variable is not working right, so I'm wondering, would anyone be able to assist me to help me get that variable processing correctly? Code: <script language="javascript"> fields = 1; function addMore() { if (fields = 1) { document.getElementById('addedMore').innerHTML = "<input type='text' name='field2' size='25' /> <span>Field 2.</span>"; fields = 2; } else if (fields = 2) { document.getElementById('addedMore').innerHTML = "<input type='text' name='field3' size='25' /> <span>Field 3.</span>"; fields = 3; } else { document.getElementById('addedMore').innerHTML = "Only 3 fields are allowed."; document.form.add.disabled=true; } } </script> Here is the code in my HTML - Code: <input type="text" name="field1" size="25" /> <span>Field 1.</span> <div id="addedMore"></div> <p class="addMore"><form name="add"><a onclick="addMore()">Add another field.</a></form></p> Thank you very much. Hello. I have a textarea where user can select a text then copy to clipboard (using EditArea script for highlighting) I need append additional information to the selected text so it won't be visible in the textarea, but only appear after copied to clipboard. For example: Code: text line one text line two text line three user selected word "two", hit CTRL+C to copy into clipboard (or used browser's context menu), but in clipboard it should be saved as: "selected word 'two'" What would be the approach? Thank you. Hi, I have a form setup so that selecting a radio button opens a specific text field. The problem is if the user starts to enter information, then switches to a different radio button (perhaps they chose the wrong radio to start), the text they already started to enter on the previous textfield doesn't get cleared. This will be a problem later when inserting to sql. Here is a summary of the code: Code: <head> <script type="text/javascript"> function doClick(objRad){ if (objRad.value=="0"){ document.getElementById("textbox").style.display='block'; } else{ document.getElementById("textbox").style.display='none'; } if (objRad.value=="1"){ document.getElementById("textbox1").style.display='block'; } else{ document.getElementById("textbox1").style.display='none'; } if (objRad.value=="2"){ document.getElementById("textbox2").style.display='block'; } else{ document.getElementById("textbox2").style.display='none'; } } </script> </head> <body> <form action="insert.php" method="post"> <p>Please choose the business type: <input type="radio" name="rad" value="0" onclick="doClick(this)"> Sole Proprietorship <input type="radio" name="rad" value="1" onclick="doClick(this)"> Partnership <input type="radio" name="rad" value="2" onclick="doClick(this)"> Corporation <div id="textbox" style="display:none"> <input type="text" name="txt"> This is my sole proprietorship info.</div> <div id="textbox1" style="display:none"> <input type="text" name="txt"> This is my partnership info.</div> <div id="textbox2" style="display:none"> <input type="text" name="txt">This is my corporation info. </div> </form> </body> Any help is appreciated, Thanks in advance! Hi. I have developed a form all working fine but am struggling on the following. I need a checkbox for an item, when the checkbox is true the user can enter text in a text box, only if the checkbox is ticked. I have numerous items I need for this. Hope someone can help. Thanks Hi, I'm hoping this will be an easy one for you experts out there. I am working on a convention registration form. What I'm trying to do is have the text "50.00" pop into the COST field when any text (even if it's only one character) is entered into the NAME field. If the text in the NAME field is deleted, the "50.00" should also be deleted. Does anyone have a script that does this? Thanks! im clueless when it comes to javascript, but this is what im trying to find: i have a page that has multiple text areas, i also have 4 links that when clicked i want to add some predefined text into the active text area the one with the text cursor in? The Hyperlinks: Code: <a href="#sc">Shift Changed</a> | <a href="#ol">On Leave</a> | <a href="#ot">OverTime</a> | <a href="#ss">ShiftSwap</a> The textareas: PHP Code: <td><textarea name=details$i rows=4 align=absmiddle cols=16 value="$details[$i]\">$details[$i]</textarea></td> as you can see the textareas are created on the fly based on a php array. would anyone be willing to put some code together for this? or even give me the basics on how i can get the active textarea's name so i can pass that to a script that inserts into a given field? Hi, Here's a sample form: Code: <form action="#" method="post"> Name:<br /> <input type="text" name="name" value="your name" /><br /> E-mail:<br /> <input type="text" name="mail" value="your email" /><br /> <input type="submit" value="Send"> </form> When you tab to a text input, the value gets highlighted. How can it be disabled? Any help is appreciated! Mike Hi, I am wanting to insert text into a text area when a button or link is clicked. I know how to replace the whole lot in the text area, but I want it to insert text where the flashing cursor is in the text box. (Like Wikipedia) If any one can help, Then I will be very, very greatfull. Thank You. hey im new to javascript and im working in this function , i can't get it work here is my code PHP Code: <SCRIPT LANGUAGE="JavaScript"> function text(form1) { var j = document.form1.diary.value; j = j.italics(); document.form1.diary.value = j ; } </SCRIPT> whats the problem ? I need to replace the captions with simple text for download speed purposes. This is controlled by a piece of Javascript written for me. I need to change the caption image for a text one. The site is here Frank alias effpeetee. Hello everyone. I was wondering if anyone could help me come up with a script for fading text in and out then changing the text to something different when it fades back in again. I want to overlay a series of quotes on my website across the top and have them display for about 15 seconds then fade out to a new quote and have it cycle through them all indefinitely. I am pretty new to javascript and not much more experienced in html in general, so please be as thorough as possible. I'm a true noob Thanks!
Hi again For instance I want to display some announcements from an external text file on a predefined box, like lets say 140 wide and height scalable according to text. The texts would be some announcements from me, a few words. I want the text justified and with previous / next link, if possible. And when they navigate to another page, they will get a random message in that box. There are news tickers on the net, but I just want something simple like this, no scrolling, fading, etc.. Has anyone a clue how I can accomplish that? Thanks. I have a form with two text input boxes. I want to limit the text typed into the boxes as follows: 1. Either box can have the number 2 typed in, but the other box must remain empty OR 2. Each box can have the number 1 typed into Is this possible with JS and, if so, how do I do it?? |