JavaScript - Inserting Variables Into Document.form.name.value
So I've got an order form that uses javascript to immediately calculate the price as soon as you enter the quantity that you want to order:
Code: <form method="post" action="submitted.html" name="form"> <table id="order-table"> <tr> <td>Widgets</td> <td><input type="text" name="quantityWidgets" onchange="calculate()"></input></td> <td> x </td> <td>$650</td> <td>=</td> <td><input type="text" name="totalWidgets" disabled="disabled"></input></td> </tr> </table> </form> And here's the Javascript Code: function calculate(){ total = document.form.quantityWidgets.value * 650; document.form.totalWidgets.value = total; } But that's just for 1 row and I have several rows of products. I could just use a different calculate() function for each row, but I'm trying to pass arguments into calculate() AND make it display the result in the proper place, but my syntax must be wrong. Code: ... <td><input type="text" name="quantityWidgets" onchange="calculate(quantityWidgets, 650, totalWidgets)"></input></td> ... function calculate(quantity, price, total){ total = document.form.quantity.value * price; document.form.total.value = total; } Can anyone help me please? Similar TutorialsSo im working on a case where I need to make a random ad pop up every time I refresh that web page. I've got all the steps down except to where I need to insert a command to write the text to the document. *Insert a command to write the following text to the Web document: Code: <a href="URL"> <img src="adn.jpg" alt="description" /> </a> where URL is the value of the rLink variable, n is the value of the rNumber variable, and description is the value of the rAD variable. what I have so far (all steps completed) is Code: <script type="text/javascript"> var rNumber= randInt(5);// generate a random integer from 1 to 5. var rAd= adDescription(rNumber);// description of the random ad. var rLink= adLink(rNumber);// URL of the random ad. document.write('<a href'"+rLink+"'><img src="ad'+rNumber+'.jpg" alt="'+rAd+'"/></a>'); </script> the bolded is the question, I know some quotes are out of order but I cant figure out which. any help would be appreciated Hi, I've been looking around the web and forums for the past few days now. Javascript is not my forte but I am beginning to understand more of it. My issue is perhaps in the method that I have tried to concatenate a variable to my radio buttons. The scenario: I have about 10 groups of radio buttons , each with 4 options. radioname1[0] radioname1[1] radioname1[2] radioname1[3] radioname2[0] radioname2[1] etc... I know how to put a variable into Code: document.form.radioname1[i].value but what I need to know is how to assign a variable to the radioname, so that I can iterate through each radionameVARIABLEHERE[i].value ? I have been looking and trying to for days. All I really need to know is how to get a variable into the radiobutton name. I know how to use a for loop to get this to iterate through it all but its just the syntax that isn't playing nice. Any help on this would be much appreciated ASAP P.S. I have seen this solution Hi, I am using javascript to collect hidden form elements and then send the values to a php page, without reloading or changing the page. Basically i have a list of job vacancies, with just an apply button, when a person clicks on the apply button, the form collects some information from their account and sends it using the javascript code. Now this works fine, except for if somebody then clicks on the second job it sends the information from the first form again. And it seems to remember the information. What i am trying to find out is it possible to have multiple forms on one page which once submitted send variables to javascript which then send them to a php but clear themselves so that it doesn't remember the previous values. Here is the code for the form: Code: <form id="submit" method="post"> <fieldset> <input type="hidden" name="usern" id="usern" value="php variable" /> <input type="hidden" name="joborder_id" id ="joborder_id" value="php variable" /> <input type="hidden" name="site_id" id ="site_id" value="php variable" /> <button class="button positive">Apply For This Job </button> </fieldset> </form> <div class="success" style="display: none;">Your Application has been sent.</div> </form> And the Javascript code is he Code: $(document).ready(function(){ $("form#submit").submit(function() { // we want to store the values from the form input box, then send via ajax below var usern = $('#usern').attr('value'); var joborder_id = $('#joborder_id').attr('value'); var site_id = $('#site_id').attr('value'); $.ajax({ type: "POST", url: "apply.php", data: "usern="+ usern +"& joborder_id="+ joborder_id +"& site_id="+ site_id, success: function(){ $('form#submit').hide(function(){$('div.success').fadeIn();}); } }); return false; }); }); Any ideas or suggestions would be appreciated, Thanks Lee Quote: <html> <head> <script type="text/javascript"> var a = january var b = febuary var c = march function test() { document.getElementById("test").innerHTML=a; <!-- needs to be A on first function click, b on second, c on third, etc..^^--> } </script> </head> <body> <p id="test"></p> <!-- the <p> should go to next month upon each function --> <input type="button" value="asdf" onclick="test()" /> </body> </html> I tried to explain it pretty well in the comment tags, thanks! Hello, I know very little Javascript, so the answer to this is probably VERY simple :-) I have some javascript working out the current date for me. I want to output this date into the value of an input field in a form. My javascript to output the date is: Code: <script type="text/javascript"> <!-- var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() document.write(month + "/" + day + "/" + year) //--> </script> And I'd like this to output in the value of the input field below: Code: <input type="hidden" name="date" value="js date to go here"/> I've tried using script tags and document.write, but it messes up my HTML and I'm not sure what exactly it is that I'm doing wrong!! Sorry for the silly question... Thanks for your help Nicky I'm trying to insert a form into a div, based on what the user selects in another select form. I'm getting an error whenever I try typing my code. I'm doing this in Dreamweaver and it highlights my text green (starting at the first /td and ending at the next / of the next /td) Will adding this form even function properly when I send? Any ideas on what's going on? Thanks Code: function showTable(y) { if(y == "readers") { document.getElementById("next").innerHTML = "<form id='readersTable'> <table> <tr> <td>Attribute 1</td> <td><input type='text' name='at1'> </td> </tr> </table> </form>" } Getting a document.form is undefined when calling this select onchange event Code: function submitShipMethodChange(form,field){ if(document.form['shipform'].elements['shipping_method']){ var fldIndex = form.elements[field].selectedIndex; var selectedMethod = form.elements[field].options[fldIndex].value; document.forms['shipform'].elements['shipping_method'].value = selectedMethod; document.forms['shipform'].submit(); } } <select name="shipping_method" onChange="submitShipMethodChange(this.shipform,'shipping_method');"> I'm having a heck of a time getting a code to populate several hidden fields in the following form that I'm extracting from the url. It is posting to the appropriate CGI, however the hidden variables are not submitted. Code: <script type="text/javascript"> str=document.URL; temp = str.split('//'); temp1 = temp[1]; temp2 = temp1.split('.'); temp3 = str.split('/'); domain = ('http://' + temp3[2]); returnthanks = (domain); association = temp2[0]; poibegin = (str.lastIndexOf('/')+1); poiend = (str.lastIndexOf('.')); poi = (str.substring(poibegin)); </script><script language="JavaScript" type="text/javascript"> function doSubmit(){ document.w2lForm('00N30000004C6Rg').value=association; document.w2lForm.('00N30000004C2u2').value=poi; document.w2lForm.('retURL').value=returnthanks; document.w2lForm.submit(); } </script> <h6 style="text-align: center">Submit your information and a professional will contact you.</h6> <form method="post" action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" name="w2lForm"> <input type="hidden" name="oid" value="005040020320220B3eR" /> <label for="first_name">First Name</label> <input id="first_name" maxlength="40" name="first_name" type="text" /><br /> <label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" type="text" /><br /> <label for="email">Email</label><input id="email" maxlength="80" name="email" type="text" /><br /> Best Time to Call:<select id="00N30000004NaDm" title="Best Time to Call" multiple="multiple" name="00N30000004NaDm"> <option value="(select at least one)">(select at least one)</option> <option value="Before 9AM (EST)">Before 9AM (EST)</option> <option value="9AM to 1PM">9AM to 1PM</option> <option value="1PM to 6PM">1PM to 6PM</option> <option value="After 6pm">After 6pm</option> </select><br /> <input id="00N30000004C2u2" type="hidden" name="00N30000004C2u2" /> <input id="00N30000004C6Rg" type="hidden" name="00N30000004C6Rg" /> <input type="submit" name="submit" value="Submit Request Information" /> </form> I wanted to know if document.getElementById works while accessing form elements. I tried doing this just for testing purposes This code doesnt work function validateForm() { var val = document.getElementById("id_login").getAttribute("value"); alert(val); return false; } but this does function validateForm() { alert(document.myForm.text_login.value ); return false; } Why doesnt document.getElementByid work with form objects.it works with all non form HTML objects.. Hi ! i sometimes are able to alter simple javascript, but i need help with the following. MaxMind.com has a javascript or geotargetting. It displays the country where ur at. I need this output in a form, so i can store it in a database. this is the script i use: <script language="JavaScript" src="http://j.maxmind.com/app/country.js"></script> <br>Country Code: <script language="JavaScript">document.write(geoip_country _code());</script> <br>Country Name: <script language="JavaScript">document.write(geoip_country _name());</script> How can i get the country name to be displayed in an input field ? something like: <form name="xx" action="somepage.htm" method="post"> <input type="Text" size="50" name="country" value="country name"> </form> My code is here and it works ... However, I would like my dynamic table to show on the same page as my body and not on a new blank page. I have created a DIV and try playing around with the document.getElementById('monTab').innerHTML but it's not working out for me ... What am i missing ? Regards, Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>new Script - Javascript Cours 11</TITLE> <META content="text/html"; charset="UTF-8" http-equiv="content-type"> <SCRIPT type="text/javascript"> function createTable(){ var Etudiant = new Array(Number(prompt("How many Students will you put in ?",""))); document.write("<table border=\"1\">"); for (var i=0; i<Etudiant.length; i++) { Etudiant[i] = window.prompt("S'il vous plait entrez le nom d'un etudiant " + (i+1) + ".","") alert("Nice to see you "+Etudiant[i]); document.write("<td>"+Etudiant[i]+"</td>"); j = parseInt(prompt("Combien de notes voulez vous calculez ?")); for (h=0;h<j;h++){ notes[h] = parseInt(prompt("S'il vous plait entrez la "+(h+1)+" note de "+Etudiant[i])); document.write("<td>"+notes[h]+"</td>"); } document.write("<tr>"); } document.write("</tr>"); document.write("</table>"); document.getElementById('monTab').innerHTML=Etudiant; } </script> <BODY> <H1>Combien de note voulez vous cumulez ?</H1> <br> <br> <input type="button" name="btnSubmit" value="TRY IT" onclick="createTable()"> <div id="monTab" size="10"> Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ... </div> </BODY> </HTML> I need help troubleshooting javascript I am using to concatenate two fields in a form and populating a hidden field with them. Here's the javascript: <script type="text/javascript"> function combName() { var fname = document.forms[0].fname.value; var lname = document.forms[0].lname.value; document.forms[0].name.value = fname + " " lname; } </script> Here are the form fields: <input id="fname" name="fname" type="text"> <input id="lname" name="lname" type="text" onKeyUp="combName()"> <input name="name" type="hidden" id="name" size="20" maxlength="80"> <input name="Submit" type="submit" id="OnSubmit" value="OnSubmit"> What am I doing wrong? Thanks. Using the url test.html?a=john&b=doe I am using the following code: <script type="text/javascript" language="javascript"> function getUrlVars() { var map = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi; function(m,key,value) { map[key] = value; }); return map; } var john=getUrlVars(map[a]); alert (getUrlVars(map[a])) alert (john) </script> I can't seem to get an alert to show me anything. The eventual result is not an alert, but a document.write to post the url contents to various form fields for firstname= and lastname= etc. I am really having a hard time using the document.write function, and am not sure how to employ it, whether in the same script statement or in a separate script in the body. Hi, I am quite new to javascript but I'm quite sure this problem is very easy to solve. I have a vague idea of what might be going wrong but I have no idea what I should be doing instead. Here is what I'm trying to do: User inputs X and Y coordinates into form validate that they are numbers do a little bit of maths redirect to a php script with a single variable tacked onto the end of the url Here is the form code: Code: //part of a larger php script to make the form echo "<form name='gotoForm' onsubmit='return coordCalc()'> <fieldset> <legend>Go to Square</legend> X <input type='text' id='X' size='1' maxlength='3'/> Y <input type='text' id='Y' size='1' maxlength='3'/> <input type='submit' value='Go To' /> </fieldset> </form> "; which references these functions in the header: Code: //Is it a number function isNumeric(elem, helperMsg){ var numericExpression = /^[0-9]+$/; if(elem.value.match(numericExpression)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } //deal with the input, check if they are both numbers with the above function //if they are do some maths on the input //add the result onto a url and redirect function coordCalc (){ var Xcoord = document.getElementById('X'); var Ycoord = document.getElementById('Y'); if(isNumeric(Xcoord, "Please enter a Number for X")){ if(isNumeric(Ycoord, "Please enter a Number for Y")){ //Takes the X and Y coordinates and transforms them into a single number //not fully coded in case you try putting some numbers into the formula btw :) var X = parseInt(document.getElementById('X').value); var Y = parseInt(document.getElementById('Y').value); var G = 16; var Z = (((G + 1) - Y) - Y); var A = (Y + Z); var B = (X - 1); var L = ((A * (G - 1)) + B); window.location = "map.php?start=" + L; } } return false; } The number validation works but the url ends up as map.php?start=NaN. Now, this simply must be the way I am assigning the Xcoord and Ycoord variables with the document.getElementByID() function I am sure. But like I said, I have no idea what to do instead, any help is massively appreciated. Thankyou! Hello, I have several event handlers on my page; one of them doesn't work. I'll post them all here, indicating how they are executed: #1: Works [CODE]<script type="text/javascript"> function combName() { var fname = document.forms[0].fname.value; var lname = document.forms[0].lname.value; document.forms[0].name.value = fname + " " + lname; } </script>[CODE] NOTE: The "lname" field uses "onKeyUp" to make the script run. A hidden field named "name" is populated with the concatenation. #2: Works (this script is too long to post; it is a validation script and runs via the "onSubmit" in the form tag.) #3: Doesn't Work [CODE]<script type="text/javascript"> function combPrograms() { document.forms[0].00N70000002WNax.value = "1st Choice: " + document.forms[0].sf1.value + document.forms[0].alameda1.value + document.forms[0].sanmateo1.value + document.forms[0].marin.value + document.forms[0].cc1.value + "2nd Choice: " + document.forms[0].sf2.value + document.forms[0].alameda2.value + document.forms[0].sanmateo2.value + document.forms[0].marin2.value + document.forms[0].cc2.value; } </script>[CODE] Note: 1. Not all of the values for Choice 1 and Choice 2 are populated. 2. For that reason, one of the later, unrelated text fields in the form has "onKeyUp="combPrograms()" to run the event. 3. A hidden field named 00N70000002WNax is populated with the concatenation. Any help is much appreciated! I'm sure this is a simple one. I have a results page, pulled from a mysql database using _GET variables from a form. On this page is a second form to refine the results by reloading the page. I want each form element item to remember/prepopulate with what it's related $_get variable is.
Hi, firstly I apologise if the title of this thread isn't quite accurate. I'm sure you've all heard it before but I am a complete newbie to Javascript so again: apologies if this is boring and tiresome to even read, let alone help with! I have been asked to make some changes to a form that uses Javascript for the form validation. There is a 'function' that contains the variables of the various form fields and then further code to raise 'alerts' if one of the fields on the form hasn't been filled in. My problem is that for some reason I am unable to add an extra variable to this code after the field of 'County' (this will hopefully make sense when you see the code / link...) and I am stumped as to why. I am able to add variables for all of the other required fields except for 'Postcode' after 'County'. This is the case for both forms. The link is he http://samdelara.co.uk/previews/banq...ation-form.htm and the code I am trying edit is below: function checkAvailibility() { // re-calculate... calculate (); if ( isName() && isAddress1() && isTown() && isCounty() && isPostcode() && isYourEmail() && isFuncDate() && somethingToQuoteFor() && isYourEmailValid() ) { document.ordersummary.emailQuote.value = "No"; setValue(); return true; } else { return false; } } function isName() { if (document.ordersummary.Name.value=="") { alert ("\n Please Enter Your Name") document.ordersummary.Name.focus(); return false; } return true; } function isAddress1() { if (document.ordersummary.Address1.value=="") { alert ("\n Please Enter Your Address") document.ordersummary.Address1.focus(); return false; } return true; } function isTown() { if (document.ordersummary.Town.value=="") { alert ("\n Please Enter Your Town") document.ordersummary.Town.focus(); return false; } return true; } function isCounty() { if (document.ordersummary.County.value=="") { alert ("\n Please Enter Your County") document.ordersummary.County.focus(); return false; } return true; } function isPostcode() { if (document.ordersummary.Postcode.value=="") { alert ("\n Please Enter Your Postcode") document.ordersummary.Postcode.focus(); return false; } return true; } function isYourEmail() { if (document.ordersummary.YourEmail.value=="") { alert ("\n Please Enter Your Email") document.ordersummary.YourEmail.focus(); return false; } return true; } function isFuncDate() { if (document.ordersummary.FuncDate.value=="") { alert ("\n Please Enter Your Function Date") document.ordersummary.FuncDate.focus(); return false; } return true; } function isemailonly() { if (document.ordersummary.emailonly.value=="") { alert ("\n Please Enter Your Email Address") document.ordersummary.emailonly.focus(); return false; } return true; } Any help with this would be very much appreciated and once again, I apologise for my distinct lack of knowledge in this area! Sam |