JavaScript - How To Generate Textfield With A Button
Hello,
I'm looking to create a web form with various text field to get input from users. Upon enter data to each field, users should have option to genenrate another text field underneath (i.e. a sub criteria of the above field). This is what i have so far, but i cannot get each button to generate the textfield right underneath. Here's my code: <html> <head> <title>test</title> <script> var maxFieldWidth = "500"; function addRow(element, indentFlag) { var elementClassName = element.className; // this is the class name of the button that was clicked var fieldNumber = elementClassName.substr(3, elementClassName.length); // current textfield width, take off the px on the end var fieldWidth = document.getElementById("textfield" + fieldNumber).style.width; fieldWidth = fieldWidth.substr(0, fieldWidth.indexOf("px")); // get the new field number (incremented) var newFieldNumber = ++fieldNumber; // get new field width in case we are indenting var newFieldWidth = fieldWidth; if(indentFlag) newFieldWidth = fieldWidth - (newFieldNumber * 10); var rowContainer = element.parentNode; // get the surrounding div so we can add new elements // create text field var textfield = document.createElement("input"); textfield.type = "text"; textfield.setAttribute("value", "textfield " + newFieldNumber); textfield.id = "textfield" + newFieldNumber; textfield.setAttribute("style","width:" + newFieldWidth + "px; margin-left: " + (maxFieldWidth - newFieldWidth)); // create buttons var button1 = document.createElement("input"); button1.type = "button"; button1.setAttribute("value", "++"); button1.setAttribute("onclick", "addRow(this, false)"); button1.className = "row" + newFieldNumber; var button2 = document.createElement("input"); button2.type = "button"; button2.setAttribute("value", "+"); button2.setAttribute("onclick", "addRow(this, true)"); button2.className = "row" + newFieldNumber; var button3 = document.createElement("input"); button3.type = "button"; button3.setAttribute("value", "-"); button3.className = "row" + newFieldNumber; // add elements to page rowContainer.appendChild(document.createElement("BR")); // add line break rowContainer.appendChild(textfield); rowContainer.appendChild(document.createTextNode(" ")); // add space rowContainer.appendChild(button1); rowContainer.appendChild(document.createTextNode(" ")); // add space rowContainer.appendChild(button2); rowContainer.appendChild(document.createTextNode(" ")); // add space rowContainer.appendChild(button3); } </script> </head> <body> <div id="main-container"> <div> <input type="text" id="textfield1" value="textfield 1" style="width:500px;" /> <input type="button" class="row1" value="++" onclick="addRow(this, false)"> <input type="button" class="row1" value="+" onclick="addRow(this, true)"> <input type="button" class="row1" value="-"> </div> </div> </body> </html> Similar TutorialsHey folks, I have a little problem here, I have two textfield and a botton Text fields: Code: <input name="rate" type="hidden" id="rate" value="<%= rs_rating.Fields.Item("rate").Value %>" /> <input name="plus" type="hidden" id="plus" value="1" /> <input name="total" type="hidden" id="total" /> <input type="Submit" name="Submit" id="Submit" value="Course Rating " /> Now what I want is something like this input "rate" value + input "plus" value which means if for example if rate (with dynamic value which could be any number) is 4 it will be 4+1 (1 is the static value of input "plus") which will be 5 and that 5 would be the value of input "total" that is the only text field which submit to DB. Just need the JS for that math problem. Thanks folks. Hi all. I have a textfield on my form problem. My boss wants it as a textfield to keep the field small on the page but still wants to be able to read all of the information that is in the field. What can I do with this?? Can we have change to textarea when he hovers to clicks in it. Any help would be appreciated. Hello, I am fairly new to javascript and am having trouble clearing a text field. I am making a "vending machine simulator" as an exercise. I have a button that is the coin return, and should clear the balance display. e.g the machine shows a balance of $2 inserted, the coin return button should clear it to 0. I believe I have done everything correctly but get NaN as the output (a text field named "box1"). I believe it has something to do with how the field is validated. It has number precision set to 2, which I would think would just become 0.00 or something. Anyways, here is my code: HTML: Code: <input class="money" id="coinreturn" type="button" value="return" onClick="coinReturn()" /> <input name="box1" id="box1" onkeypress="return numbersonly(this, event)" maxlength="4" type="text"/> JavaScript for the coinReturn function: Code: function coinReturn() { document.getElementById('box1').value = 0; } JavaScript for the "numbers only" validation: Code: function numbersonly(myfield, e, dec) { var key; var keychar; if (window.event) { key = window.event.keyCode; } else if (e) { key = e.which; } else { return true; } keychar = String.fromCharCode(key); // control keys if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) { return true; } // numbers else if ((("0123456789.").indexOf(keychar) > -1)) { return true; } } anyone would you help me I have some problem to calculate PHP Code: <table width="415" cellspacing="1" cellpadding="1"> <tr> <th width="151" scope="col">Price</th> <th width="189" scope="col">qty</th> <th width="63" scope="col">total</th> </tr> <tr> <td><label> <input name="price" type="text" id="price" value="2000"> </label></td> <td><label> <input name="qty" type="text" id="qty" value="2"> </label></td> <td><label> <input name="total" type="text" id="total" value="4000"> </label></td> </tr> <tr> <td><input name="price" type="text" id="price" value="2000"></td> <td><input name="qty" type="text" id="qty" value="4"></td> <td><input name="total" type="text" id="total" value="8000"></td> </tr> <tr> <td> </td> <td>Total</td> <td><label> <input name="gtotal" type="text" id="gtotal" value="12000"> </label></td> </tr> </table> How to calucate it from price * qty = Total and gtotal=sum of total, in the first rows. Ican do it if there is one row, but i dont know how to calculate samae name of text field like in 2nd or more rows Hi, The following code tests the 'numbers' field for numbers. It generates an alert if the field contains anything but numbers. Code: <script type='text/javascript'> function isNumeric(elem, helperMsg){ var numericExpression = /^[0-9]+$/; if(elem.value.match(numericExpression)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } </script> <form> Numbers Only: <input type='text' id='numbers'/> <input type='button' onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only Please')" value='Check Field' /> </form> However, if I want to change the above statement to read if the textfield does not equal to a number, return true, else, alert the user and return false. I change the branch statement and have it read "if(!(elem.value.match(numericExpression)))," to try to do this. But, the alert message does not generate if I enter "abc123" even though that value clearly contains numbers. Not sure what is going here. Please help me. Thank you. Hey Forum, I have this little script working great when all if's are seperated. what i need is to have the script read if one of these fields has a value then result=true. as you see the script now i have this seperated in many different if's for all three text fields. I need to put all text fields like they were one, thanks Code: function ValidateLink(Form) { var result = true; if (formMyLocations.cellAddress.value.length == 0) { alert('Cell address is empty on the locations page'); result = false; } if (formMyLocations.homeAddress.value.length == 0) { alert('Home address is empty on the locations page'); result = false; } if (formMyLocations.carAddress.value.length == 0) { alert('Car address is empty on the locations page'); result = false; } return result; } Hi, I have a text field on which I am writing onChange event. The value of text field is getting changed by calander popup by selecting date from calander, but onChange event is not fired unless I manually change the text field, how can we force the event to fire when date is selected by calander popup. any hints please...?? here is my onchange code Code: <input type="text" onChange="changeFinishDates(this.value, this.name)" id="statusFinishDate<%=currModule%><%=bomGroupName%>" name="statusFinishDate<%=currModule%><%=bomGroupName%>" value="<%=statusFinishDateYYYY%>" class="input_med" maxlength="10" title="Enter Promise Date in MM/DD/YYYY format"> <a href="#" onClick="javascript:calPrmdate.select(Status_Dates.statusFinishDate<%=currModule%><%=bomGroupName%>,'anchorPrmdate','MM/dd/yyyy'); return false;" name="anchorPrmdate" id="anchorPrmdate">select</a> <input type="hidden" value="<%=bomGroupName%>" name="bomGrp<%=currModule%>" id = "bomGrp<%=currModule%>" /> Hello Friends... I am working on parsing the querystring using javascript So I have successfully parsed the querystring & also replace the special character+ but I want to display the querystring in the TextField Control So how to implement it Please guide me.... So I have the below form. How can I validate that if the user enters a quantity in the field they also must select a reason code. How can I do this.\ Code: <form name="theForm" onsubmit="validate()"> <table> <tr id='Child1'> <td><input type = 'text' MAXLENGTH='3' id='PROD_1_150.00_133.50' style = 'width:40px'; class='textfield' name='quantity[]' ></td> <td><select size='1' STYLE='width: 130px' class='smallDrop' name='reason_codes[]'><option value='' > - SET REASON - </option> <option value='BROKEN' >BROKEN</option> <option value='ENTERED' >ENTERED</option> </select></td> <input type='hidden' value='4395' name='itemid[]' > </tr> <tr id='Child2'> <td><input type = 'text' MAXLENGTH='3' id='PROD_1_150.00_133.50' style = 'width:40px'; class='textfield' name='quantity[]' ></td> <td><select size='1' STYLE='width: 130px' class='smallDrop' name='reason_codes[]'><option value='' > - SET REASON - </option> <option value='BROKEN' >BROKEN</option> <option value='ENTERED' >ENTERED</option> </select></td> <input type='hidden' value='4396' name='itemid[]' > </tr> <tr id='Child3'> <td><input type = 'text' MAXLENGTH='3' id='PROD_1_150.00_133.50' style = 'width:40px'; class='textfield' name='quantity[]' ></td> <td><select size='1' STYLE='width: 130px' class='smallDrop' name='reason_codes[]'><option value='' > - SET REASON - </option> <option value='BROKEN' >BROKEN</option> <option value='ENTERED' >ENTERED</option> </select></td> <input type='hidden' value='4353' name='itemid[]' > </tr> <tr id='Child4'> <td><input type = 'text' MAXLENGTH='3' id='PROD_1_150.00_133.50' style = 'width:40px'; class='textfield' name='quantity[]' ></td> <td><select size='1' STYLE='width: 130px' class='smallDrop' name='reason_codes[]'><option value='' > - SET REASON - </option> <option value='BROKEN' >BROKEN</option> <option value='ENTERED' >ENTERED</option> </select></td> <input type='hidden' value='4355' name='itemid[]' > </tr> </table> </form> Is it possible to have an onclick event on a readonly input field? I want to avoid the mouseover event.
How would you create a text field that allows users to select different search engines, and then search their keyword. Essentially what I'm looking for is a something similar to the default multiple search field that you see in Firefox and Safari. At this point in time I have the multiple search feature implemented. But all I really have is a <select> next to a <input>. What I want is something that combines the two. Basically I want the look and feel of Firefox/Safari default multiple search field. Any help would be appreciated. Thanks, Hello. I have a for loop, that automatically creates textfields and checkboxes. These of course have the same name and I am using request.getParameterValues to retrieve them. However, I am struggling on this rather simple issue. I am no javascript guru, so maybe you guys could help me out. I have tried googling for this answer, but havent found anything that can help my problem exactly. I have three text fields on each row and one checkbox. I would like for each checkbox I click, that the textfields to be enabled. A sample code he Code: <td><input type="text" size="10" name="pris" disabled="disabled"></td> <td><input type="text" size="10" name="toppfart" disabled="disabled"></td> <td><input type="text" size="10" name="motorinstallasjon" disabled="disabled"></td> <td><input type="checkbox" name="motorCheckbox" value="<%=motor.getId()%>" onclick="(pris.disabled=!this.checked)& (toppfart.disabled=!this.checked)& (motorinstallasjon.disabled=!this.checked)"> </td> However this doesnt quite work :-) I have tried with document.Form.pris but that didnt help either. Hi I have a problem but im not sure if it could be solved by Javascript or PHP but either is good. I have a form with a text box where users can enter a 10 digit code. The 10 digit code can be just numbers/letters (eg. 0123456789 or abcdefghij) or it can be a mixture (eg. 0a1b2c3d4e). What I would like is if the user enters a code which is a mixture of numbers and letters then the submit url goes to mixed.php If the user enters a code which just has letters or numbers then the submit url goes to sole.php Anybody can help me? I know its possible to change the url from selecting radio buttons so I hope this can be achievable. I hope that makes sense...let me know if you need more info. Thanks guys! 1 Textfield counter problem. I have this counter (in javascript) that counts the characters in my textarea, and displays the amount of characters left to type. When a limit is reached, the last typed character gets deleted automatically. Now the counting goes fine everywhere, but when a page loads, the counters do not count the amount of characters in it, only when the script is activated by a 'onkeydown'. Now the thing is, I would use an onload, but the problem is that the ID's of these things are random, so I don't know how to do this... Here's the code: Code: <script type='text/javascript'> function textCounter(field, counter) { var max='140'; if (field.value.length > max) field.value = field.value.substring(0, max); else counter.value = max - field.value.length; } </script> PHP Code: <?php while($row = mysql_fetch_array($result)) { echo "<form name='form".$row['photoid']."'>"; echo "</td><td>"."<textarea rows='5' cols='30' onkeydown=\"textCounter(this.form.a".$row['photoid'].",this.form.counter);\" onkeyup='textCounter(this.form.a".$row['photoid'].",this.form.counter);' id='a".$row['photoid']."' name='".$row['photoid']."' \">".$row['description']."</textarea>"; echo "U heeft nog <input readonly type='text' name='counter' size='3' maxlength='3' value='140' /> tekens over."; echo "</form>"; } ?> Short extra explanation: onkeydown and onkeyup activate the counter of that form. The photoid's get loaded from a directory, and can be fully random. 2 Coloring problem on change This problem is in the same script as the previous problem, but I've torn the code separate so that is easier for you to inspect and understand it's function. So it is about the same textarea as the previous problem. The textaera is in a table, and there is another cell in that table that changes it's color. When the page is loaded, all the colors are green. When you click a textarea, the color turns orange, but if you click somewhere else it turns orange again. But when you are focused on the textarea (and the color is orange) and you press a key, the color changes red and stays red even when you lose focus on that element. Now what I want is that when you activate the textarea and press an arrow on your keyboard (and thus you don't change the contents of the textaerea) the color stays orange, and doesn't change red... How should this be done? Here's the simplified code: Code: <script type='text/javascript'> function changeColor(id,color,type) { if(type== 'focus' || type == 'keydown'){ document.getElementById(id).style.borderColor=color; }else if(type=='blur' && document.getElementById(id).style.borderColor == 'orange'){ document.getElementById(id).style.borderColor='green'; }else{document.getElementById(id).style.borderColor='red';} } </script> PHP Code: <?php while($row = mysql_fetch_array($result)) { echo "<form name='form".$row['photoid']."'>"; echo "<table>"; echo "<tr><td>"."<img src='fotos/" . $row['photoid'] . $row['type'] . "' />"; echo "</td><td>"."<textarea rows='5' cols='30' onkeydown=\"changeColor('c".$row['photoid']."','red','keydown');\" id='a".$row['photoid']."' name='".$row['photoid']."' onfocus=\"changeColor('c".$row['photoid']."','orange','focus')\" onblur=\"changeColor('c".$row['photoid']."','red','blur')\">".$row['description']."</textarea>"; echo "</td><td id='c".$row['photoid']."' style='border:2px solid green;' >This cell get's colored"; echo "</td><td></tr></table>"; echo "</form>"; } ?> Hi guys I have a little problem. I have 2 textfields and I need to change a link href based on whatever is in those textfields. The link has to be in this format: Code: <a class="display" href="test.php?test1=textfield1&test2=textfield2">test</a> The reason for this is I need to launch fancybox and as far as im aware this is the best way to call it. So what im hoping is when I add type something in textfield1 this will be reflected in the url. And same for textfield2. I know this can be done easily if I submit the page but I dont want that. Hopefully some javascript/ajax can help me out? I hope im being clear but let me know if im not. Thanks so much! hi , i am trying to pass a javascript variable into URL ..but not getting the value.. var newID = 2; row.insertCell(6).innerHTML = "<a href=Update_Gate.cfm?GateIdVar=newID>Edit Gate</a>"; i am getting "newID" in Update_Gate.cfm page ... i want to pass this javascript variable in url and read it on the other page as form field value any help !! i'm trying to "post" the selected checkboxes (name and value) into a mysql database; i've search "docter google" for some answers and read that the "easiest" way to do this is to collect the checkboxes and put the values and names in to an hidden textfield ( function setvalue() ) and collect this textfield in a new page with php; but this doesn't work. it seems that the variable naamtest is empty (when i load a word into this var arv it does work) can someone find my error? Code: <script type="text/javascript"> function shift(which) { var counter = 0; // counter for checked checkboxes var i = 0; // loop variable var veck = "";// final url variable var beginsaldo = 79; var namen_array = ""; var va_array = ""; var namen = ""; var valu = ""; var input_obj = document.getElementsByTagName('input'); // get a collection of objects with the specified 'input' TAGNAME for (i=0; i < input_obj.length; i++){ // loop through all collected objects if (input_obj[i].type == 'checkbox' && input_obj[i].checked == true){ // if input object is checkbox and checkbox is checked then ... counter++; // ... increase counter var valcheck = input_obj[i].value; // waarde van de geselecteerde checkbox in variable steken test = Number(valcheck); // absolute rekenwaarde van variable maken beginsaldo -= test; //beginsaldo verminderen met de checkbox value if (counter > 0){ var naam = input_obj[i].name + ' '; var val = input_obj[i].value + ' '; namen = namen + naam; valu = valu + val; namen_array = namen.split(" "); va_array = valu.split(" "); var naamtest = namen_array; }}} if (counter > 10){ alert ("u mag maar 10 renners selecteren"); var tr = which.parentNode; while ( tr.tagName.toUpperCase() != "TR" ) { tr = tr.parentNode; if ( tr == null ) return; // something went very wrong! } var tds = tr.getElementsByTagName("td"); tds[0].getElementsByTagName("input")[0].checked = false; } else if (beginsaldo < 0){ alert ("uw saldo is te laag om dit uit te voeren"); var tr = which.parentNode; while ( tr.tagName.toUpperCase() != "TR" ) { tr = tr.parentNode; if ( tr == null ) return; // something went very wrong! } var tds = tr.getElementsByTagName("td"); tds[0].getElementsByTagName("input")[0].checked = false; } else{ var tr = which.parentNode; while ( tr.tagName.toUpperCase() != "TR" ) { tr = tr.parentNode; if ( tr == null ) return; // something went very wrong! } var tds = tr.getElementsByTagName("td"); if ( tds[0].getElementsByTagName("input")[0] == which ) { tds[2].innerHTML = tds[0].innerHTML; tds[0].innerHTML = " "; tds[3].innerHTML = tds[1].innerHTML; tds[1].innerHTML = " "; tds[2].getElementsByTagName("input")[0].checked = true; } else { // this code is optional!! tds[0].innerHTML = tds[2].innerHTML; tds[2].innerHTML = " "; tds[1].innerHTML = tds[3].innerHTML; tds[3].innerHTML = " "; tds[0].getElementsByTagName("input")[0].checked = false; } function setValue() { var arv = naamtest.join(); // This line converts js array to String document.f2.arv.value=arv; // This sets the string to the hidden form field. }}} </script> I am building a search function for a sermon blog on squarespace. The format url for category search is www.mysite.org/blog?category=... I have drop down menus and a datepicker but need to figure out how to get java to build the url and redirect to it. The output url should look like http://www.mysite.org/sermons?catego...+mybook+mydate. Any help would be greatly appreciated. Here is my java so far. Code: function search() { var myRootSite = 'www.mysite.org/sermons?category='; var myNewTitle = document.getElementById('myTextField').value; var mySpeaker = document.getElementById('mySpeaker').value; var myService = document.getElementById('myService').value; var mySeries = document.getElementById('mySeries').value; var myBook = document.getElementById('myBook').value; var myDate = document.getElementById('myDate').value; if (myNewTitle == 0 && mySpeaker == 0 && myService == 0 && mySeries == 0 && myBook == 0 && myDate == 0) { alert('Please enter some search criteria'); return; } var title = document.getElementById('title'); title.innerHTML = mySpeaker; } Hi, I am trying to develop a quiz system.Here is a simplified version of my code with lots of modifications made mainly for easier comprehension. Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <script type="text/javascript"> var count=3; var questions = new Array(3); for (var i = 1; i <=count; i++) { questions[i] = "Question "+String(i); } </script> <script type="text/javascript"> /* Global "swap" holder Use value, null, if no layer initially visible */ var currlayerId = "question1"; function toglayer(id) { if(currlayerId) setDisplay(currlayerId, "none"); if(id)setDisplay(id, "block"); currlayerId = id; } function setDisplay(id,value) { var elm = document.getElementById(id); elm.style.display = value; } </script> <style type="text/css" media="screen, print, projection"> .text { display:none; } </style> </head> <body> <li><a href="#" onclick="toglayer('question1');return false;">Q.1</a></li> <li><a href="#" onclick="toglayer('question2');return false;">Q.2</a></li> <li><a href="#" onclick="toglayer('question3');return false;">Q.3</a></li> <div id="question1" class="text"> <script>document.write(questions[1]);</script> <br> </div> <div id="question2" class="text"> <script>document.write(questions[2]);</script> </div> <div id="question3" class="text"> <script>document.write(questions[3]);</script> </div> <script>toglayer('question1');</script> </body> </html> When you will run the code,you will find three links Q.1 Q.2 Q.3 These links are basically due to the following code Code: <li><a href="#" onclick="toglayer('question1');return false;">Q.1</a></li> <li><a href="#" onclick="toglayer('question2');return false;">Q.2</a></li> <li><a href="#" onclick="toglayer('question3');return false;">Q.3</a></li> When you click on any of the links,you will see the question associated with it.This is because of the following code Code: <div id="question1" class="text"> <script>document.write(questions[1]);</script> <br> </div> <div id="question2" class="text"> <script>document.write(questions[2]);</script> </div> <div id="question3" class="text"> <script>document.write(questions[3]);</script> </div> -------------------------------------------------------------------------------------- Now,here's the question,how do I put the div layers and their links in a loop.That is,I want it to be something like this Code: (for var i=1;i<=count;i++) { <li><a href="#" onclick="toglayer('question[i]');return false;">Q.[i]</a></li> } Code: (for var i=1;i<=count;i++) { <div id="question[i]" class="text"> <script>document.write(questions[i]);</script> <br> </div> } The reason why I ask this is,is because the number of questions in the quiz depends on the value of the javascript variable "count".Therefore,if the value of count is 5,the number of questions would be 5,if it is 6 the number of questions will be 6,etc. Thanks Hemant Hello, I'm a complete javascript beginner so sorry if I'm just being stupid I wanted to know if it's possible for me to add a picture (as in a .png file) to a page by clicking on something which would then make an image appear on the screen (which I would then want to be able to drag and move but I made a different thread for that question) I hope this explanation made sense Thank you for your time |