JavaScript - Maximum And Minimum.
I have a script that allows someone to set a value that has both a maximum and a minimum. They click a [+] and [-] button that increases or decreases the value until it hits either the max or the min. The problem is, I can't come up with an if statement that does this right.
Code: var = controls; controls = functions() { if (object.x > 1 && object.x < 50) { ...do something... } }; This is a dumbed-down version. This function is actually part of an object and "object.x" is something else in the object. "...do something..." increases or decreases the value of "object.x" when a user clicks a button. This doesn't work though. if it hits either 1 or 50, it breaks because one or the other becomes false. Similar TutorialsI have been trying to get this to work for the last few hours with no luck. I'm convinced there's something obvious that I am missing but I just cannot see it! For info, the "minimumQuantity" is a <span id> generated by my client's site's eCommerce software, for example: Code: <span id="minimumQuantity">8</span> I've checked that this is the only span with this ID on the product detail page. If there is no minimum quantity, "Not applicable" is generated instead of a number. Under the example of a minimum quantity of 8: The error is displayed if you enter 7 or less (including minus numbers) into the quantity field. This is correct. The error is NOT displayed if you enter 8 or 9 into the quantity field. This is correct. The error IS displayed if you enter between 10-79 in the quantity field. This is NOT correct. The error is NOT displayed if you enter between 80-99 in the quantity field. This IS correct, but makes no sense! Code: Code: function checkQuantity() { var minimumQuantity = document.getElementById("minimumQuantity").innerHTML; if(minimumQuantity != "Not applicable") { var quantityEntered = document.getElementById("qty").value; parseInt(minimumQuantity); parseInt(quantityEntered); if(quantityEntered < minimumQuantity) { alert("The minimum order for this product is "+minimumQuantity+" units.\n\nYou entered "+quantityEntered+". Please correct the quantity and try again."); return false; } } } Hey all- trying to get this bit of js to work properly. I'm close, but the issue is that if the window is opened at a size that is SMALLER than the original image, it wont scale down to the smaller size until i first make the window bigger, THEN size it down smaller. The image itself is big (its fashion photography so it needs to be high res) and the js sets the minimum width at 1070. Id like to have the image as big as possible and then on page load automatically drop down to as low as 1070px if the window size is small. Here is the js: Code: jQuery.noConflict(); function screenSize() { var w, h; w = ( window.innerWidth ? window.innerWidth : ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth)); h = ( window.innerHeight ? window.innerHeight : ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight)); return {w:w, h:h}; } jQuery(document).ready(function () { if( screenSize().w > 1070 ) { document.getElementById('home-page-bg-image').style.width = screenSize().w + 'px'; } }) jQuery(window).resize(function() { if( screenSize().w > 1070 ) { document.getElementById('home-page-bg-image').style.width = screenSize().w + 'px'; } }); let me know if you have any ideas This 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> 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); } |