JavaScript - Is It Possible To Assign Values To Document.getelementsbyname('field')
I have to update the rowid(assigned to checkbox) whenever the textbox value is updated and this needs updating value of document.getElementsByName('rf_select[]') when checkbox value matches the textbox id. There can be mutliple number of rows each with a uniqueID (checkbox value) that matches with the textboxID onload.
Code :- <td align="center"><input type='checkbox' name='rf_select[]' id='rf_select' value="rowId" CHECKED border="none"/> <td align="left"><input type='text' name='policy_no' id="rowId" size='25' value="test" onBlur=handleRowId('policy_no'); /></td> <script language="javascript"> function handleRowId(){ var arr = new Array(); var rowarr = new Array(); arr = document.getElementsByName('policy_no'); rowarr = document.getElementsByName('rf_select[]'); for(var i = 0; i < arr.length; i++) { var obj = arr.item(i); //alert(obj.id + " = " + obj.value); for(var j=0; j< rowarr.length; j++){ if(rowarr[j].value == obj.id){ document.getElementsByName('rf_select')[j].value = rowarr[j].value+"#@#"+obj.value; } } } } </script> I am having problem assigning values here - document.getElementsByName('rf_select')[j].value = rowarr[j].value+"#@#"+obj.value; Any help is appreciated. Similar TutorialsI am trying to pass two inputs one hidden one text to php using document.getelementsbyname(). The hidden input is in a php array, the text input is a user typed input. When ever I pass it to php it always pulls the first item in the array. So even if I click "View" on the 3rd item in my array it will still show the two inputs from my first item in the array. Here are my html input fields followed by java function followed by submit. How do i get it to send the proper information and not the first item in the array. Code: <input name="imageNumber" type="hidden" value="<? echo $imageNumber; ?>.png"> <input type="text" name="imprint" value="" size="40"> <script> function passImprint() { imageNumber = (document.getElementsByName("imageNumber")[0].value); imprint = (document.getElementsByName("imprint")[0].value); urlString = "imprint.php?imprint=" +imprint + "&imageNumber=" +imageNumber; window.open(urlString,"width=640,height=295"); } </script> <input type="button" value="View Imprint" onClick="passImprint();"> I am trying to add a green tick or red cross to my form so that the user will know if they have filled out the form correctly before submitting it and getting told that they need to fill out this or correct that. I have added NAME to each form field and have been using this to read the content but for some reason I am unable to read what is in the field I get the error, 'undefined'. this is what code I have started on... Code: function checkValidFormInput(id, noCheck) { if (noCheck == '') { var idValue = document.getElementsByName(id); if (id == 'customerName') { alert(id); alert(idValue.value); if (idValue != '') { document.getElementsByName(id + 'Img').innerHTML = '<img alt=/"/" src=/"/">'; } } if (id == 'customerEmail') { if (idValue != '') { document.getElementsByName(id + 'Img').innerHTML = '<img alt=/"/" src=/"/">'; } } } } my code is still in the early stages and is missing the other parts to place a red cross, as the moment i am working on placing the green tick. Can anyone spot what I have wrong in my code that is causing it not to read the form field. my form field is coded as so.. Code: <li class="value"> <input type="text" autocomplete="off" name="customerName" value="<?=$fullname;?>" style="width: 200px;" onChange="checkValidFormInput(this.name, '');"> <div style="float: left;" id="customerNameImg"></div><br style="clear:both"> </li> I'm having problems with selecting values from array. I have a dropdown box where you choose what fruit you want to buy. When selected the array should assign 2 values to that fruit. I don't know how to do that. Here's what I have.. I added comments. Javascript part: Code: <script type="text/javascript"> function Fruits() { var selectfruit = newArray( //assigning values to fruit selected from dropdown box newArray("Banana", 1, 1), newArray("Apple", 1.2, 0.5), newArray("Mango", 1.1, 0.9), newArray("Orange", 0.1, 9.99)); var howmanyfruits = Number(document.getElementById("howmanyfruits").value); // how many fruits are you buying var totalfruitsowned = Number(document.getElementById("totalfruitowned").value); // How many fruits do you already have /* cash and coupons needed to buy fruits. cash is cpst for 1 fruit. coupons is cost for 1 fruit cash_all is cost for all fruits you're buying coupons_all is cost for all fruits you're buying each fruits requires cash AND coupons to be bought. Cash and coupons are tied to the first values in Array. Eg. If you choose Apple that value would be 1.2 The 'fruitsmaxtobuy' variable is not tied to the first value, but the second one in array. If you choose Apple that value would be 0.5. */ var cash = Math.round(((totalfruitsowned * 0.51 * selectfruit) + 700)*10)/10; var coupons = Math.round(((totalfruitsowned * 0.51 * selectfruit) + 850)*10)/10; var cash_all = Math.round((howmanyfruits * cash)*10)/10; var coupons_all = Math.round((howmanyfruits * coupons)*10)/10; var fruitsmaxtobuy = Math.round((totalfruitsowned * 0.12 * selectfruit)*10)/10; /* Display Error if nothing is entered or if you forget to enter total fruits */ if (((howmanyfruits=="" || howmanyfruits==null) && (totalfruitsowned=="" || totalfruitsowned==null)) || ((howmanyfruits==Number(document.getElementById("howmanyfruits").value)) && (totalfruitsowned=="" || totalfruitsowned==null))) {document.getElementById("cash").innerHTML = "Error"; document.getElementById("coupons").innerHTML = "Error"; document.getElementById("cash_all").innerHTML = "Error"; document.getElementById("coupons_all").innerHTML = "Error"; document.getElementById("fruitsmaxtobuy").innerHTML ="Error"} else { document.getElementById("cash").innerHTML = cash; document.getElementById("coupons").innerHTML = coupons; document.getElementById("cash_all").innerHTML = cash_all; document.getElementById("coupons_all").innerHTML = coupons_all; document.getElementById("fruitsmaxtobuy").innerHTML =fruitsmaxtobuy} } </script> HTML part: Code: <form action="" id="fruitcost"> <table align="center" width="37.5%" cellpadding="0" cellspacing="0"> <tbody> <tr> <th colspan="2" align="center">Fruit cost calcultor</th> </tr> <tr> <td>Select Fruit:</td> <td align="center"><select id="selectfruit"> <option>Banana</option> <option selected>Apple</option> <option>Mango</option> <option>Orange</option> </select> </td> </tr> <tr> <td>Total Fruits Owned:</td> <td align="center"><input id="totalfruitsowned" type="text" /></td> </tr> <tr> <td>How many fruits are you buying:</td> <td align="center"><input id="howmanyfruits" type="text" /></td> </tr> <tr> <td>Money Needed to buy 1 fruit:</td><td><font id="cash"></font></td> </tr> <tr> <td>Coupons Needed to buy 1 fruit:</td><td><font id="coupons"></font></td> </tr> <tr> <td>Money Needed:</td><td><font id="cash_all"></font></td> </tr> <tr> <td>Coupons Needed:</td><td><font id="coupons_all"></font></td> </tr> <tr> <td>Nr. of fruits you can buy:</td><td><font id="fruitsmaxtobuy"></font></td> </tr> <tr> <td align="center" colspan="2"><input type="button" value="Submit" onclick="Fruits()" /></td> </tr> </tbody> </table> </form> hi guys i hava gridview gvcustomers with following fields latitude,longitude,customer and i have a javascript function in my aspx page fucntion Customer() { AddCustomer(lat1,long1)--- row1 of gridview lat long AddCustomer(lat2,long2)--row2 of gridview lat long } in the above function i would like to loop through the gridview and get the latitude and longitude colmns in the function instead of hardcoding as i have been doing now...thank you i advance EDIT: goto post #7 here goes JS code: (keep an eye on codes of type: document.getElementById('footerX').innerHTML=X Code: function details() { var choo = document.getElementById("choo"); aa = choo.chooserver.value; bb = choo.chooversion.value; if(aa==1) {k=0} else if(aa==2) {k=1} else {k=false} pa= domain[k].getElementsByTagName("server") ; pb=""; pc = domain[k].getElementsByTagName("version"); pd= domain[k].getElementsByTagName("date"); pe= domain[k].getElementsByTagName("month"); pf= domain[k].getElementsByTagName("year"); ta = (bb==1)? " T4 ":(bb==2)? " T3.6 " :(bb==3)? " T2.5 " : 100 ; versions(ta); document.getElementById('footer1').innerHTML='one'; } function versions() { for (i=0;i<=pc.length;i++) { a = pc[i].childNodes[0].nodeValue; b = pa[i].childNodes[0].nodeValue; comparedate(d,g,f); dates(i, a); document.getElementById('footer2').innerHTML='two'; if (a==ta) { addto() document.getElementById("mc"+i).innerHTML= txt ; document.getElementById("md"+i).innerHTML= txt2 ; } document.getElementById('footer3').innerHTML='tee'; } document.getElementById('footer4').innerHTML='fou'; } HTML: Code: <div id = "conten2"> <table id="tb1" border="1px"> </table> </div> <div id = "footer"> feet </div> <div id = "footer1"> feet1 </div> <div id = "footer2"> feet2 </div> <div id = "footer3"> feet3 </div> <div id = "footer4"> feet4 </div> <div id = "footer5"> feet5 </div> Desired Output: feet one two tee fou feet5 Output: feet feet1 two tee feet4 feet5 Area of error predicted: inside the for loop, the statement immediately after loop is not executed once loop ends, nor does it return to original function for execution of other statements which were written after the function containing the loop was called.... Hello, I'm Stephen Martin, an undergraduate Psychology student and researcher. Although I am a self-proclaimed tech geek, the extent of my knowledge ends abruptly at any sort of coding. I need help from anyone who is willing. The paradigm: I am currently investigating the decision making strategies in a particular population (I cannot reveal too much, lest our study be scooped by competing researchers in the field). The paradigm that we use is built through a package called MouseLabWeb (http://www.mouselabweb.org/). MLWeb basically provides an interface for researchers to create a html/php/js/css website that presents a grid (table). A small example is shown on the aforementioned website. The javascript functions allow each participant's events to be recorded, timestamped, and written to a MySQL database. This allows the researcher to know which cells the participant viewed, in what order, for how long, and ultimately to which decision the participant came. Additionally, the PHP/HTML creates the table, but the data within it is populated by the javascript. This is a blessing and a curse. Here is why. The problem: The javascript offered by MLWeb allows the cells to be randomized. This is necessary, because in such paradigms, counter-balancing is required, otherwise there would be order effects (e.g., people tend to automatically read from the upper left) that are not controlled for. So the order of the columns and rows are randomized, the data which is then placed into the html/php table. For the current study, we want the cells corresponding to particular types of information to be shaded differently. For example, we'd want cells that contain bad information to be dark, cells that contain neutral info to be grey, and cells that contain good information to be white. The problem is that setting these values (style="background-color: #XXXXX) only applies to the table as created by the PHP. It only affects the position, and does not consider the information that it is filled with. We want the information that a cell is filled with to dictate what background-color the cell is. Example of the problem (A,B,C are the different types of info, with the surrounding character implying shading) What we want: Unrandomized: [.A.] [|B|] [#C#] Randomized: [#C#] [.A.] [|B|] What the problem is: Randomized: [.C.] [|A|] [#B#] Again, thank you for reading this long post. I'm up for any suggestions! TL;DR The website uses PHP to render a table and javascript to populate it. Javascript randomizes the column and row order upon each visitation, thus randomizing the cells. How do I make javascript issue a background-color change to each cell according to the information that it is populated with. 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! I'm new to JavaScript, just started an adult education class in it. I wrote a simple script to view a list of document properties and their values. This part works in Internet Explorer: for (var prop in document){ p_prop = prop; p_list[i] = p_prop + " : " + document[prop] + "<br />"; but doesn't in FireFox. I was wondering how to get the value of a property in FireFox. Thanks. After some severe experimentation I got to googling for an answer and stumbled upon these forums. So my big question is, is it possible to use getElementsByName() with a variable as the parameter? And if not what is another way I can get at each element based on a variable list of names. Code: var curObjSet = document.getElementsByName(String(idStack[y])).item(0) I'm trying desperately to make my code organic and not to have to hard code field names but without this working its leaving me with out options. Thank you for any time and help provided. - Eric Hi, I'm trying to use the getElementsByName method in a div tag and it's retrning an error. Can anyone suggest how to access it from a javascript function Hi, I am setting up an email form & i would like the subject field to hold multiple values, the forrm will be something like this: Name:Mr A Date: 01/01/01 Reason:Football I want the form to send an email without opening an email client & I would like the subject field to look like this: Subject: Mr A, 01/01/01, Football or Subject Mr A - 01/01/01 - Football Can anyone tell me if this is possible, if if is can you please point me in the right direction. Thanks Can someone help me I am trying to get these two values, firstname and lastname to both go into the same output box on a form. I want them to show up alongside one another like so nameform.output2.value=firstname + " " + lastname; but the values are assigned within seperate if statements as you can see below.. so I dont know how to get them to merge.. I am kinda new with javascript so any help will be very useful. Thakns very much. Code: function checkform(nameform){ var firstname=new Array(); firstname[0]="johndefinition"; firstname[1]="jamesdefinition"; var lastname=new Array(); lastname[0]="smithdefinition"; lastname[1]="simpsondefinition"; if (document.getElementById("namebox").value.indexOf("John")!=-1) {nameform.output2.value= firstname[0];} if (document.getElementById("namebox").value.indexOf("James")!=-1) {nameform.output2.value= firstname[1];} if (document.getElementById("namebox").value.indexOf("Smith")!=-1) {nameform.output2.value=lastname[0];} if (document.getElementById("namebox").value.indexOf("Simpson")!=-1) {nameform.output2.value=lastname[1];} } Hi all, I'm a beginner in JavaScript can someone please help me to resolve this issue and thanks in advance for your help! What I'm trying to do is.. When you select multiple <div> get the value of each one and save in separate hidden fields accordingly. What needs to happen is... Click div 1 --> if hidden field1 value is "none", save there, else check hidden field 2 if that's "none" save in hidden field 2 Click div 2 --> if hidden field1 value is "none", save there, else check hidden field 2 if that's "none" save in hidden field 2 Here's what I've come up with so far (currently I'm using JQuery) The problem with this is that no matter what div I click in which order it only updates the 1st hidden field Code: <div id="click1" name="click1" value="123" onclick="abc()"/>Click 1</div> <div id="click2" name="click2" value="225" onclick="def()"/>Click 2</div> <input id="passenger_1" name="passenger_1" type="hidden" value="none" /> <input id="passenger_2" name="passenger_2" type="hidden" value="none" /> <script> function abc() { var pasvalue = $('#click1').attr('value'); var pas1_value = $('#passenger_1').attr('value'); var pas2_value = $('#passenger_2').attr('value'); if(pas1_value == "none"){ $('#passenger_1').attr('value', pasvalue); }else if (pas2_value == "none"){ $('#passenger_2').attr('value', pasvalue); }} function def() { var pasvalue = $('#click2').attr('value'); var pas1_value = $('#passenger_1').attr('value'); var pas2_value = $('#passenger_2').attr('value'); if(pas1_value == "none"){ $('#passenger_1').attr('value', pasvalue); }else if (pas2_value == "none"){ $('#passenger_2').attr('value', pasvalue); }} </script> I'm trying to add the ui autocomplete function to some form fields using a facebook style, but I'm not sure how to get more than one value to actually pass to the form field. As my script is now it only takes the last selected value into the hidden form field. Here's my current script: Code: <div id="editcbprofileinterests"> <div id="formWrap"> <div id="interests" class="ui-helper-clearfix"> <input id="cb_intereststerm" type="text"> <input id="cb_interests" name="cb_interests" type="hidden"> </div> </div></div> <script type="text/javascript" src="autocomplete/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="autocomplete/js/jquery-ui-1.8.custom.min.js"></script> <script type="text/javascript"> $(function(){ //attach autocomplete $("#cb_intereststerm").autocomplete({ //define callback to format results source: function(req, add){ //pass request to server $.getJSON("autocomplete/autocominterests.php?callback=?", req, function(data) { //create array for response objects var suggestions = []; //process response $.each(data, function(i, val){ suggestions.push(val.cb_intereststerm); }); //pass array to callback add(suggestions); }); }, //define select handler select: function(event, ui) { //create formatted interest var interest = ui.item.value, span = $("<span>").text(interest), a = $("<a>").addClass("remove").attr({ href: "javascript:", title: "Remove " + interest }).text("x").appendTo(span); //add interest to interests div span.insertBefore("#cb_intereststerm"); //add interest to hidden form field $('input[name=cb_interests]').val(interest); }, //define select handler change: function() { //prevent 'to' field being updated and correct position $("#cb_intereststerm").val("").css("top", 2); } }); //add click handler to activities div $("#interests").click(function(){ //focus 'to' field $("#cb_intereststerm").focus(); }); //add live handler for clicks on remove links $(".remove", document.getElementById("interest")).live("click", function(){ //remove current activity $(this).parent().remove(); //correct 'to' field position if($("#interests span").length === 0) { $("#cb_intereststerm").css("top", 0); } }); }); </script> 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> Hello again eveyone it is the new guy with yet again asilly question. OK here goes.... ..... is it posable to have JS assign a sequental value as the ID for rows in a table. As some of you might know I have an MS Excel spread sheet with data in it that I am wanting to load into a web page. I have worked out how to get the data from Excel but I want to create a JS Function that will read the file then create a table using a template like Clone table but I will need each row entered from the Excel file to have a different ID from the prev entry and different than the next entry. Here is an example of what I'm talking about so that I am as clear as I can be. Quote: Function read () { Reads row 1 from Excel file} Function filltemplatetable (){ Take the data from Function read and placed it in a template table that I have created} Function fillmaintable() {Takes the newly created table object and inplant it in the maintable as a row} Function assignrowid () {assign the newly added row to the maintable as ID 1} Loop back to Function read () untill the last row in the Excel file is copied over to the web page. I was hopeing that the loop could creat a sequenchal numbering system that I could use later on for collaps/expand not to mention filtering but I need to make this an onload so that when I update teh Excel spreadsheet this web page will update the changes. I KNOW I KNOW I just droped a chunk on ya'll and I'm not asking for you guys to do the work for me but I am begging for some direction. I wish I had some CODE to show you guys but I don't have any that pertains to this stage in my work because i'm not even sure it is posable if it isn't then I will need to rethink my creation steps. THANKS AGIN in advance hey man,i write this code , Code: <html> <head> <script type="text/javascript"> function Do() { winow.location.assign("http://www.google.com"); } </script> <script type="text/javascript"> function replaceDoc() { window.location.replace("http://www.w3schools.com") } </script> </head> <body> <input type="button" value="Google" onclick="Do()" /> <input type="button" value="Replace document" onclick="replaceDoc()" /> </body> </html> i want to see if i click Google button I have a randomly generated Language file that corresponds to keys, the reason Ii say randomly generated is that the letter to key values used are not standard assignments. It seems the language coding is DEC in sets of 3 values by 5 .i.e. "248 036 034 036 248" = A "036 084 084 084 120" = a (See attached : ENGLISH-1.txt) How would I assign these values to a sentence lets say: All great : 248 036 034 036 248 ll gre 036 084 084 084 120 t but convert all characters to their correspondnig sets of 5 ? This then needs to get sent to a database where it is interpreted into sets of Binary to turn on and off LEDs' ... I have never worked with Javascript or LED's before, so any help is so appreciated. If I assign an array to another variable, any changes to the array change both variables. Is it a pointer instead of a copy? Try this: Code: <script type="text/javascript"> Arr=['a','b','c']; Arr2=Arr; Arr[1]='_'; alert('Array 1: '+Arr); alert('Array 2: '+Arr2); // Both arrays become 'a','_','c' </script> Why's this happen? Is it to be expected? Variables don't do this but it's happening for arrays in IE and FireFox, maybe all others. Hi all, I have a table with two columns, first is a text field (quantity) and second is product description. Then I have a JavaScript code in the header section that calculates total based on quantity X price. Right now, user has to input quantity in the text field in first column. I would like to change the first column to checkboxes, which I know how to do...problem is I don't know how to adjust the JavaScript so that it knows a checked box is equivalent to a value of 1. Here is a sample of the table code which gives me 2 cells (a blank text field, and a description cell of "A"), and the totals field at the bottom of the table: Code: <TR> <TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_A_4.99" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>A</TD> </TR> <TR><TD>TOTAL</TD><TD ALIGN="RIGHT"><INPUT TYPE=TEXT NAME=TOTAL SIZE=10 onFocus="this.form.elements[0].focus()"></TD> And here is the bit of JavaScript that calculates the table: Code: <script language="JavaScript" type="text/javascript"> <!-- function CalculateTotal(frm) { var order_total = 0 // Run through all the form fields for (var i=0; i < frm.elements.length; ++i) { // Get the current field form_field = frm.elements[i] // Get the field's name form_name = form_field.name // Is it a "product" field? if (form_name.substring(0,4) == "PROD") { // If so, extract the price from the name item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1)) // Get the quantity item_quantity = parseInt(form_field.value) // Update the order total if (item_quantity >= 0) { order_total += item_quantity * item_price Any ideas on how to change the item_quantity to always=1 so that I can change the input type from 'text' to 'checkbox'? Then, with this example, if the user were to check the box for "A" the "total" field would say 4.99 Thanks in advance! |