JavaScript - I Want Text Box In A Table To Display Maximum Number If This Number Is Exceeded
I have a function below where every time a question is submitted, it will add a new row in the table with a textbox which allows numbers entry only. My question is that I don't know how to code these features in this function:
1: I want the text box to be between 0 and 100, so if text box contains a number which is above 100, it will automatically change the number to the maximum number which is 100. Does any one know how to code this in my function below in javascript: Code: function insertQuestion(form) { var row = document.createElement("tr"); var cell, input; cell = document.createElement("td"); cell.className = "weight"; input = document.createElement("input"); input.name = "weight_" + qnum; input.onkeypress = "return isNumberKey(event)"; cell.appendChild(input); row.appendChild(cell); } Similar TutorialsThis is what I have so far which prompts the user three numbers and then displays the max of the numbers and all three of them. Now the last thing I was wanting to do is change it from three prompts to just one prompt. How could I prompt to type in three numbers in one prompt and get the max from those three numbers.? Any help would be greatly appreciated. Thank you in advance! Code: <head> <title>Maximum Number</title> </head> <body> <center><h2>Maximum Number</h2> <script language="Javascript" type="text/javascript"> function Max(num1, num2,num3) { var largest=arguments[num1, num2, num3] for (i=0; i<arguments.length; i++) { if ((num1>num2)&&(num1>num3)) largest=num1 else if((num2>num1)&&(num2>num3)) largest=num2 else largest=num3 } return(largest) } document.write("</br>") var num1=prompt("Enter the first number: ", "") var num2=prompt("Enter the second number: ", "") var num3=prompt("Enter the third number: ", "") var max= Max(num1, num2, num3) document.write("You entered: ", num1, ", ", num2, ", ", num3) document.write("</br>") document.write("The Maximum number is: ", max) </script></center> </body> </html> When I used toFixed() method on a number, I thought that this method round a number to a specified approximation, but I got a surprising result, the number became string! 15.23689 .toFixed ( 2 ) ==> "15.24" So does it convert the number into string? I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
Just wondering what the code is for a phone number to be displayed nn-nnnnnnnn for example: 67 43289482 current code is Code: var pn = document.contact_form.phonenum.value; if ((pn.length != 10) || (/[^0-9]/g.test(pn))) { // 10-digit phone number alert ( "Please fill in your 10-digit telephone number" ); return false; } So I've got my code ready to generate a random number between a certain range and printing it out in text. What I'm trying to accomplish with this code is to print the actual image as well as the text. I'm aware that there is an error in my document.write line, and that it's not the best solution. I was hoping for some suggestions to change/improve this last bit. Thanks in advance! Code: <html> <title>RANDOM IMAGE TO NUMBER RANGE</title> <body> <center> <script language="javascript" type="text/javascript"> var ranges = [ { from: 11, image: "image4.jpg" }, { from: 1, image: "image3.jpg" }, { from: -9, image: "image2.jpg" }, { from: -20, image: "image1.jpg" } ]; var random_num = (Math.round((Math.random()*41)+-20)); var image = "default"; for(var i = 0; i < ranges.length; ++i) { if(random_num >= ranges[i].from) { image = ranges[i].image; break; } } { document.write("Text: " + random_num + " Text, " + " Image: = " + image); { document.write('<img src="images/' + ranges[i].image); } } </script> <br></br> <div> CLICK <a href="javascript:history.go(0);">REFRESH or F5</a> FOR A RANDOM IMAGE on NUMBER-RANGE. </div> </center> </body> </html> So never done this one before so I am having a bit of a time trying to figure this one out. Say I have a list of LI element and only a few of them have a inline style of display block, the rest have a display of none. How would I go about looping through and dertmining the number of elements that have a display of block? Code: for(var m = 0; m < li.style.display.length; i++) { alert(m); }; The above is just crazy but where my mind melts.. I would like to be able to write a function (or something) to count the number of rows in the table, I don't know how to do it, any help is appreciated.
Hi, Have a dynamic table in JS that adds rows. Want a max of 8 rows. How can that be done? [CODE] <script language="Javascript" type="text/javascript"> function addRow() { var tbl = document.getElementById('ReqDtTbl'); var lastRow = tbl.rows.length; var iteration = lastRow; var row = tbl.insertRow(lastRow); var cellLeft = row.insertCell(0); var textNode = document.createElement('input'); textNode.size = 7; textNode.name = 'startdate' + iteration; cellLeft.appendChild(textNode); var cellRight = row.insertCell(1); var el = document.createElement('input'); el.type = 'text'; el.name = 'enddate' + iteration; el.id = 'enddate' + iteration; el.size = 7; cellRight.appendChild(el); // the last cell! var cellRightSel = row.insertCell(2); var sel = document.createElement('select'); sel.name = 'TypeHrs' + iteration; sel.options[0] = new Option('-Select-', '""'); sel.options[1] = new Option('Comp Time', 'Comp Time'); sel.options[2] = new Option('Credit Hrs', 'Credit Hrs'); sel.options[3] = new Option('Overtime', 'Overtime'); sel.options[4] = new Option('Rel Comp', 'RelComp'); cellRightSel.appendChild(sel); var cellRight = row.insertCell(3); var el = document.createElement('input'); el.type = 'text'; el.name = 'No. of Hours' + iteration; el.id = 'No. of Hours' + iteration; el.size = 7; cellRight.appendChild(el); } [CODE] John I have a phone number field on my form that needs validation, but I'm not sure how to code this. I have the following function to validate a first name is entered and last name. The phone number field must match a 7 digit or 10 digit(with area code)phone number. I want to be able to include paranthese and/or hyphens for the valid phone number. function checkForm1() { if (document.forms[0].firstname.value.length == 0) { alert("You must put in a first name"); return false; } else if (document.forms[0].lastname.value.length == 0) { alert("You must put in a last name"); return false; } Hi, I have been here before and was responded to quickly with professional results, and you guys have managed to bring me back! Anyway, I am horrible at coding, and I need to know how to write this, basically when the page loads a random number is generated <script type="text/javascript"> window.onload=function() { var random = Math.rand() * (6 + 1); Then later in the page if (random = 1) { document.write("1") //I'm not sure if thats the code to show simple text, but I'm pretty sure it is, correct me if I'm wrong } if (random = 2) { document.write("2") } if (random = 3) { document.write("3") } and so on so forth. I will be changing the numbers in the script to my accord and use of the script, so please use the //notes to mark where and what things is, with a brief explanation, so I don't have to unscramble the code to find out what it is. Thanks fellow nerds! Here's part of the code, problem I have is I have it set up for 5 rows, I need it to be able to make more than this, but not sure how many more, this is dependent on user input, how should I go about this? Also how would I go about making the users orders correspond with the right item number? I'd need to sort the array somehow? document.write('<table border="1">'); document.write("<tr>"); document.write('<th colspan="5">ORDER</th>') document.write("</tr>"); document.write("<tr>"); document.write('<th align="left">Item #</th>'); document.write('<th align="center">Name</th>'); document.write('<th align="center">Price</th>'); document.write('<th align="center">Quantity</th>'); document.write('<th align="center">Total</th>'); document.write("</tr>"); document.write('<td align="center">0</td>'); document.write('<td>' + productName[0] + '</td>'); document.write('<td> $' + sausagePrice + '</td>'); document.write('<td> ' + orderItemQty[0] + '</td>'); document.write('<td> ' + sausageTotal + '</td how to change placeholder text colour in javascript? Code: <script> // this is inside a html form form['height'].placeholder+="enter password"; </script> how do I change the text above from grey to a different colour? Also, how do I check for isnotanumber in a form submission? Code: if(isNaN(form['weight'].value)) { console.log('use a number'); it still outputs that console.log error!!!!!!!!! I am delving into the coding world and while I understand the basic principle of cookies, conditional statements, arrays, etc... I am still learning how to properly implement them. Any asistance with the following situation would be greatly appreciated and help with my learning as I try to reverse engineer the logic. I have looked around the web and this forum with little success. If the situation below is too complicated, I would really appreciate even a shove in the right direction regarding the logic. ----------------- How could I show a preset counter which counts up from a preset, beginning number toward a preset, end number? I imagine the increment and speed is set be the difference between the two numbers and a timeframe. Assumptions: I would rather not set the increment but edit the end number to show a steady increase. As I update that number, the increment adapts dynamically. I would want the number/script to be useful, so it should not refresh to the beginning number on each page load (i.e. num=0). When a visitor comes to the page, it must seem like the counter has been steadily been increasing in their absence. Coke or Pesi did something similar one time regarding cans sold to date (doubt it was plugged into a DB somewhere but rather based on a steady sales figure) and it was pretty cool. All the best! Hi. I'm trying to make a web page using javascript for validation, but after the last 2 functions I wrote, 2 functions don't work anymore. I attached the file so you can see for yourself. And I know there are a few things I could do to the html, but I'm saving those for last. Any idea why the scripts don't work is much appreciated. (the functions that don't work a "cont_pag1" and "cont_pag2"...btw...they worked just fine until I wrote the last few functions (the ones for the pag4 div))...And sorry for the really strange language...
I need help on some code
Code: function makefarm(){ document.getElementById("farms").value ++ ; document.getElementById("pop").value -=2 ; document.getElementById("coin").value -=10 ; document.getElementById("lumber").value -=20 ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; } function upgradefarm(){ document.getElementById("iron").value -=5 ; document.getElementById("coin").value -=20 ; document.getElementById("farmupgrades").value ++ ; } var food = function () { var f1 = document.getElementById('buttonhere'); var f2 = '<input type="button" value="Make Farm" onclick="makefarm()" /> Farms:<input type="text" id="farms" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <input type="button" value="Upgrade Farm" onclick="upgradefarm()" /> Farm Upgrades:<input type="text" id="farmupgrades" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <br/><br/><br/> Current Income:<input type="text" id="CIfarm" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/>'; f1.innerHTML = f2; } Code: Coin:<input type="text" id="coin" value=50 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Lumber:<input type="text" id="lumber" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Iron:<input type="text" id="iron" value=25 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Food:<input type="text" id="food" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> POP:<input type="text" id="pop" value=10 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> so if i press the makefarm() button it will add to the numbers but if i press it again they will got to zero how could i make it remember the numbers?(the input boxes are the second code) This is quite easy yet hard. I'm trying to apply something in my forums, where a member could post something let's say: "Hi, I am number [random]1, 6[/random]" And what would happen is that, a number from 1 to 6 would be the content, for example, it would be: "Hi, I am number 3" But if you refresh the page, it would still be the same number. Like it won't be randomized again. So how do I make a javascript/html code that would make a one time number randomizer. Hi All, I have a problem in javascript. When i am doing rounding of the value i am getting the o/p like 2.2e4. I have to display this value in decimal value(2.0002) not in exponential. Can any one please help me to resolve this issue.? Regards, VIjai I have a dynamic form that uses Code: var input_box = $("<input type=\"text\" id=\"ac\" >") I need to have a unique id for each input that is displayed. How can I do this? I can't use vars or anything in there as variables don't work in this as it is? |