JavaScript - I Want Spinner To Display 0 If Spinner Is Empty
Hi,
I have 2 questions about my spinner. first question is that if a number is removed in the spinner by backspace and that there is no number in a spinner, when I click away from the spinner, it shows an empty spinner. What I really want is that if the spinner is empty and I click away, I want the spinner to display 0 instead. How can this be done? second question is that if I enter in 00045 or 0000009 in a spinner and I click away, I want it to display 45 and 9 and not 00045 and 0000009. How can this be done as well? Below is my spinner: Code: <script type="text/javascript"> function Spinner(elem,min, max){ this.elem = elem; this.elem.value = min; this.min = min; this.max = max; this.timer; this.speed = 150; //milliseconds between scroll values var selfO = this; this.elem.onkeyup = function(){ var regex = /^[0-9]*$/; if(!regex.test(selfO.elem.value)){ selfO.elem.value = selfO.elem.value.substring(0,selfO.elem.value.length-1); return; } selfO.validateValue(); } this.validateValue = function(){ if(Number(selfO.elem.value) > selfO.max) {selfO.elem.value = selfO.max;} if(Number(selfO.elem.value) < selfO.min) {selfO.elem.value = selfO.min;} } this.stopSpinning = function(){ clearTimeout(selfO.timer); } this.spinValue = function(dir){ selfO.elem.value = Number(selfO.elem.value) + dir; selfO.validateValue(); selfO.timer = setTimeout(function(){selfO.spinValue(dir);},selfO.speed); } } window.onload=function(){ //create the Spinner objects var SpinnerHours = new Spinner(document.getElementById('txtHours'),0,23); var SpinnerMins = new Spinner(document.getElementById('txtMins'),0,59); var SpinnerSecs = new Spinner(document.getElementById('txtSecs'),0,59); //assign the scoll buttons' events var btnHoursUpO = document.getElementById('btnHoursUp'); btnHoursUpO.onmousedown = function(){SpinnerHours.spinValue(1);} btnHoursUpO.onmouseup = function(){SpinnerHours.stopSpinning();} var btnHoursDownO = document.getElementById('btnHoursDown'); btnHoursDownO.onmousedown = function(){SpinnerHours.spinValue(-1);} btnHoursDownO.onmouseup = function(){SpinnerHours.stopSpinning();} var btnMinsUpO = document.getElementById('btnMinsUp'); btnMinsUpO.onmousedown = function(){SpinnerMins.spinValue(1);} btnMinsUpO.onmouseup = function(){SpinnerMins.stopSpinning();} var btnMinsDownO = document.getElementById('btnMinsDown'); btnMinsDownO.onmousedown = function(){SpinnerMins.spinValue(-1);} btnMinsDownO.onmouseup = function(){SpinnerMins.stopSpinning();} var btnSecsUpO = document.getElementById('btnSecsUp'); btnSecsUpO.onmousedown = function(){SpinnerSecs.spinValue(1);} btnSecsUpO.onmouseup = function(){SpinnerSecs.stopSpinning();} var btnSecsDownO = document.getElementById('btnSecsDown'); btnSecsDownO.onmousedown = function(){SpinnerSecs.spinValue(-1);} btnSecsDownO.onmouseup = function(){SpinnerSecs.stopSpinning();} var scrollBtnO = document.getElementsByClassName('scrollBtn'); for(i=0; i < scrollBtnO.length; i++){ scrollBtnO[i].onclick = function(){return false;} } } </script> <form action="create_session.php" method="post"> <table> <tr> <th>Duration:</th> <td>Hrs</td> <td class="spinner"><textarea class="spinner" name="txtHours" id="txtHours" cols="2" rows="1"></textarea></td> <td><button class="scrollBtn" id="btnHoursUp"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button> <button class="scrollBtn" id="btnHoursDown"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td> <td>Mins</td> <td class="spinner"><textarea class="spinner" name="txtmins" id="txtMins" cols="2" rows="1"></textarea></td> <td><button class="scrollBtn" id="btnMinsUp"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button> <button class="scrollBtn" id="btnMinsDown"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td> <td>Secs</td> <td class="spinner"><textarea class="spinner" name="txtsecs" id="txtSecs" cols="2" rows="1"></textarea></td> <td><button class="scrollBtn" id="btnSecsUp"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button> <button class="scrollBtn" id="btnSecsDown"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td> </tr> </table> </form> Similar TutorialsI have a spinner where user enters in their value or uses the buttons to increase or decrease the value. When the user is happy with the value in the spinner the user submits the value. The value would then appears in another table inside a textbox. What my question is, is that how can I get it so that when user submits value in the other table that the spinner buttons are also displayed in the other table so that except the value is in a textbox, it is displayed in a spinner which operates exactly the same as the spinner in the previous table? Below is code where the value from the first spinner is retireved and oututted into another table into a textbox: Code: function insertQuestion(form) { var row = document.createElement("tr"); var cell, input, alertErrors = "", qnum = 1; cell = document.createElement("td"); cell.className = "weight"; input = document.createElement("input"); input.name = "weight_" + qnum; input.onkeypress = "return isNumberKey(event)"; input.value = form.textWeight.value; cell.appendChild(input); row.appendChild(cell); document.getElementById("qandatbl").appendChild(row); } Below is html code which shows 3 tables, first table is where the user enters in the value of the answer weight in the spinner, second table is the submit button (Add Question) and third table is where the value is displayed after submission of the first spinner. Code: <form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" > <div id="detailsBlock"> <table id="answerWeight"> <tr> <th colspan="3"> Answer Weight </th> </tr> <tr> <td class="spinner">Weight: <input type="text" class="spinnerWeight" name="textWeight" id="txtWeight" />%</td> <td><button class="scrollBtn" id="btnWeightUp" type="button"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button> <button class="scrollBtn" id="btnWeightDown" type="button"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td> </tr> </table> <table id="questionBtn" align="center"> <tr> <th> <input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" /> </th> </tr> </table> </div> <hr/> <table id="qandatbl" border="1"> <tr> <th class="qid">Question No</th> <th class="question">Question</th> <th class="option">Option Type</th> <th class="answer">Answer</th> <th class="weight">Weight</th> </tr> </table> </form> Hoefully that is all the information you require. If you want more information then just add a comment to me. Thank you. Hey guys, Is there a way of hiding a whole table row if a certain cell inside it is empty? Thanks, Adam Hello I am building a registration page . If the user completes all the required field i load the info into the database but if there are missing fields I combined some javascript into php to display next to the empty field a note that it should be filled. This is my code : Code: <?php if (isset($_POST['submit'])) { $username =$_POST['username']; $password =$_POST['password']; $password2 =$_POST['password2']; $firstname =$_POST['firstname']; $lastname =$_POST['lastname']; $email =$_POST['email']; $address =$_POST['address']; $city =$_POST['city']; $state =$_POST['state']; $zip =$_POST['zip']; $dob =$_POST['dob']; if (empty($username)){ ?> <script language="javascript"> var user =true </script> ;<? } if (empty($password)){ ?> <script language="javascript"> var pass =true </script><? ; } And it goes on................... Then next to the inpput box I display a text in case the user didn't fill in the info like this and by the way the above code is in my validate.php page wich i include in the main registration page Code: <?php include ('includes/validate.php');?> <form action="registration.php" method= "post" > <fieldset> <legend>sitename Registration</legend> <p> <label for"username">Username</label> <input type "text" name=username /> <script type="text/javascript"> if (user == true) { document.write("Please enter a Username of your choice"); } </script> </p> <p> <label for"password">Password</label> <input type "password" name=password /> <script type="text/javascript"> if (pass == true) { document.write("Please choose a Password"); } </script> </p> <p> <label for"password2">Re-type Password</label> <input type "pass2" name=password2 /> <script type="text/javascript"> if (pass2 == true) { document.write("Please re-type your Password"); } </script> </p> And it goes on......for the rest of the input fields. I need some help with a solution for when the user fills in the info and pushes the "register" button the page refreshes and it whipes all the info the user already input but it does display the warnings where the user didn't input any data, so what would be the best way to go to keep the data that the user entered.Thank you. Hi i would like to know how to check for any check boxes that are empty if any is empty then i want the code to stop were its at and go no ferther and prompt a message somthing missing?
Hello, I'm trying to compare a <div> background color with another color. However, on FireBug, I noticed that my <div> doesn't have a background color code in it (I placed document.getElementById(o) on watch), even though I have already specified on my css. As a result, I'm comparing an empty string with another color code. Why is this so ? Javascript: Code: function highlight(o) { var color1; color1 = document.getElementById(o).style.backgroundColor; if (color1.compareColor("#0072BC")) { document.getElementById(o).style.backgroundColor="#f8951e"; } else if(color1.compareColor("#f8951e")){ document.getElementById(o).style.backgroundColor="#0072BC"; } } HTML: Code: <div class="nav-tab" id="home"><a href="#" onmouseover="highlight('home')" onmouseout="highlight('home')" >Home</a></div> How do i declare an empty array? I need to iterate through a group of arrays and search for a user-chosen word every time. The problem is i can't declare how many array elements will be needed, such as: new array = (12), because some words inside my arrays, occur more than others. So i need to decalare an empty array with x number of possible values. Can this be done? What type of variable is an empty array element? i thought it was undefined, but i noticed that they have different behavior than undefined does: Code: var r=Array(1); var s=r.concat([0,"",null,undefined]); alert (s.toSource()) //==="[, 0, "", null, (void 0)]" typeof s[0] //==="undefined" typeof s[4] //==="undefined" as you can see, 0 and 4 both === undefined. yet, they don't have the same toSource()... why? is this special type named anything specific? or more importantly, can it be detected outside of an array as being distinct from undefined? EDIT: im thinking this would be the same type as ({}).nonProp ... i guess the question is actually, "can you tell the difference between uninitialized and undefined"? Is there a built in method to remove empty array elements
Hey folks. I currently have a PTZ camera that is taking screenshots every minute (or so) and sending a copy of that image (overwriting the previous one) to my website. Basically, the visitors to the website will be greeted with a live screenshot. The problem is if the visitor hits the website at just the right time, the screenshot won't be loaded completely while the 2nd most screenshot is being overwritten... creating an empty image load (the dreaded red X). What I am looking to do with Javascript is to be able to load a generic image in the scenario in which no image loads. Is that possible? I tried doing something like the following and onload-ing it, but it wouldn't work. Any help would be appreciated. Thanks! Code: <script type="text/javascript"> <!-- function replaceSrc() { var images = document.getElementsByTagName('img'); for(var i = 0; i < images.length; i++) { var img = images[i]; if(img.src.length == 0) { img.src = 'images/garbage.gif'; } } } --> </script> Hi. I have a form and if a field is blank a message pops up. This doesnt stop my php from running though and takes the user to another page. Is there a line to keep them on that page untill all fields are filled out? code so far is simple. <script type="text/javascript" language="JavaScript"> function nameempty() { if ( document.form.subject1.value == '' ) { alert('No name was entered!') return false; } } </script> //then down the bottom I have a small bit that implements that code Tearing my hair out here. I've got a table where I'm trying to append additional input and file fields when someone clicks a link. The appending works fine, but when I click submit, none of the appended values are being passed through. I've tried it two different ways (both being appended to the "moreSwatches" table cell: Code: function addSwatch2() { var moreSwatches = document.getElementById('moreSwatches'); imgBox = document.createElement("input"); //imgBox.type = 'file'; //imgBox.name = 'swatch[]'; imgBox.setAttribute("type","file"); imgBox.setAttribute("name","swatch[]"); moreSwatches.appendChild(imgBox); textBox = document.createElement("input"); textBox.type = 'text'; textBox.name = 'swatchname[]'; moreSwatches.appendChild(textBox); setBR = document.createElement("br"); moreSwatches.appendChild(setBR); } function addSwatch() { var moreSwatches = document.getElementById('moreSwatches'); var newDiv = document.createElement("div"); addText = '<input type="file" name="swatch[]" style="width: 300px"> <input name="swatchname[]" size="25"><br>'; newDiv.innerHTML = addText; moreSwatches.appendChild(newDiv); } If I hardcode a second set of inputs below the first, it works fine. Anything that is appended is getting ignored. Anyone got any ideas? Hi guys, I am using jquery ui to have two connected sortable lists. The first list is available numbers, and the second is selected numbers. Everything with the jquery is working fine. The problem i am having is when i try to get the contents of the selected numbers box, it is empty, even though the sortables are or were dragged to that list. I tried using this... Code: var response = $("#selected_numbers_box").val(); alert( response ); and I also tried this.. Code: var response = $("#selected_numbers_box").html(); alert( response ); both are empty. What i am trying to do is retrieve the values of the new list "Selected numbers Box". Any suggestions? My code was taken directly from the jquery ui tutorial. Thanks in advance. Hi there, I have this form that validates a few textboxes & a dropdownlist. And when it is not filled in, the border of the textboxes and dropdownlist would turn red, followed by an alert message notifying which field have not been filled in. Else, the border will revert back to black, and the form will be submitted successfully. Right now, I would like to prevent the user from entering blanks (like entering a space to cheat the system). How should I tweak the codes to make it work? Javascript Code: function check(checkForm) { var fields = new Array("Name","Email Address", "Domain Name"); var index = new Array(),k=0; for(var i=0;i<fields.length;i++) { var isFilled = false; var c = document.getElementsByName(fields[i]); for(var j = 0; j < c.length; j++) if(!c[j].value == "") { isFilled = true; c[j].className = "defaultColor"; } else { c[j].className ="changeToRed"; } if(!isFilled) { index[k++] = fields[i]; } } if(k>0) { joinComma = index.join(', '); alert('The field(s) corresponding to '+ joinComma + ' is/are not selected.'); return false; } } HTML Code: * Name: <input type="text" id="Text27" name="Name" /><br /> <br /> *Email Address: <input type="text" id="Text28" name="Email Address" /> @ <select id="Select5" name="Domain Name"> <option></option> <option>hotmail.com</option> <option>yahoo.com</option> </select> <input id="Submit5" type="submit" value="Submit" onclick="return check(checkForm)"/> Hi, I am not able to calculate the offsetwidth of the element which contains extra space in between the characters. for ex. if the element contains "A A" as a innerHTML value - if we calculate offsetwidth as 29 and also if the element contains "A A" value again I am getting the same offsetwidth as 29. While calculating the offsetwidth it is not calculating the empty space. May I know how to calculate the offsetwidth with the empty space because I need to insert the ellipsis (...) if it exceeds original width? Code snipet function fitStringToWidth(title,width,className) { var span = document.createElement("span"); span.className="titleBar-Title-1"; span.style.display='inline'; span.style.visibility = 'hidden'; span.style.padding = '0px'; document.body.appendChild(span); var result = title; span.innerHTML = result; alert("Offsetwidth : " + span.offsetWidth); .......................... } Thanks in advance. Arthik How to get this to work? Code: <html> <body> <script type="text/javascript" src="01.js"> <h1>Welcome</h1> <a href="#" onclick="write(); return false;">Click here</a> <div id="info_here"> </div> </body> </html> 01.js Code: function write() { var a=document.getElementById("info_here"); a.innerHTML=document.write("Hello <br />" + "Thanks for clicking"); } anotherVar should be -1 if there are no matches, but it doesn't seem to work
PHP Code: var string = "abadafa"; var matches = string.match(/a/gi); //doesn't work with /z/ for example var anotherVar = matches.length || -1; The default operator should return the second value if the first is null or false, and according to PHP Code: try { alert(matches.length); } catch (e){ alert(e); //alerts null } it's null. fyi: http://helephant.com/2008/12/javascr...ault-operator/ What's going on? Hi. I have a form and in the form is a checkbox use as a switch to hide and unhide a two textbox. When the other textbox is hide, the other is unhide such as the code below. When the user click the submit button on the first time and there is an error, it reloads and the id="fieldset.-in_honor" hide because it is set to hide initially but not empty. My problem is, I want to show the fieldset whichever is not empty after submit button is click and there is an error so user will not enter any more information to the other field. Is that possible? Please help. I have a hard time explaining it but hopefully you will get the idea. Thanks in advance and hope to hear from you as soon as possible. Code: /*toggle switch*/ <p><input type="checkbox" name="in_honor" value="" id="checkmemoriam" onclick="showHide(this.name);" />Please check to make this donation in honor of someone special.</p> /*hide textbox but unhide when the switch is on or the checbox is check*/ <fieldset id="fieldset-in_honor" class="fieldgroup" style="display:none"> <legend class="hide">In Honor Of</legend> <p>To make this donation in honor of someone special, please complete the two fields below:</p> <div class="formfield" <?php echo highlight('donate_honor_name'); ?>> <label for="donate_honor_name">Name:</label> <input id="donate_honor_name" name="donate_honor_name" class="text" type="text" value="<?php safeEcho($form['donate_honor_name'])?>" /> <?php helper_error('donate_honor_name'); ?> </div> <div class="formfield" <?php echo highlight('donate_honor_acknowledgement'); ?>> <label for="donate_honor_acknowledgement">Acknowledgement should be sent to:</label> <textarea id="donate_honor_acknowledgement" name="donate_honor_acknowledgement"><?php safeEcho($form['donate_honor_acknowledgement'])?></textarea> <?php helper_error('donate_honor_acknowledgement'); ?> </div> /*always show on first load of the page but once the checkbox is check, will need to hide and delete info entered*/ <fieldset id="fieldset-in_registration"> <label for="impact" style="font-weight:bold">B. Impact Areas</label> <table> <tr> <td style="width:380px;text-align:left"><label for="d" id="deslist4" name="deslist4">Crime and Drug Use <a href="javascript:alert('Ensuring prevention and treatment programs and appropriate support services are available to people who are at risk for drug addiction and supporting efforts to reduce crime.');">what is this?</a></label></td> <td style="float:right"> $<input id="d" name="d" class="text" type="text" value="<?php safeEcho($form['d'])?>" style="width:90px;" onChange="CalculateTotal()" /> <?php helper_error('d');?> </td> </tr> <tr> <td style="width:380px;text-align:left"><label for="e" id="deslist5" name="deslist5">Early Childhood Development <a href="javascript:alert('Children are healthy, loved, nurtured, safe and have access to quality childcare and early education. This includes programs that increase parenting skills, providing books to all children and providing developmental screening for 3 and 4 year olds.');">what is this?</a></label></td> <td style="float:right"> $<input id="e" name="e" class="text" type="text" value="<?php safeEcho($form['e'])?>" style="width:90px;" onChange="CalculateTotal()" /> <?php helper_error('e');?> </td> </tr></table> Code: /*JAVASCRIPT FUNCTION*/ function showHide() { if (document.getElementById('checkmemoriam').checked) { if (document.getElementById('TotalC').value == "0.00") { document.getElementById('fieldset-in_honor').style.display=""; // this is for showing document.getElementById('fieldset-in_registration').style.display="none"; } else { var answer = confirm("You entered a total gift designation of $" + document.getElementById('TotalC').value + ". At this time, gifts made \"in honor of someone special\" are not eligible for the designation option. Clicking the OK button will delete all your designation(s). Do you want to continue?") if (answer) { document.getElementById('TotalC').value="0.00"; document.getElementById('d').value="0"; document.getElementById('e').value="0"; document.getElementById('fieldset-in_honor').style.display=""; // this is for hiding document.getElementById('fieldset-in_registration').style.display="none"; } else { document.getElementById('checkmemoriam').checked=false; } } } else { if ((document.getElementById('donate_honor_name').value="") && (document.getElementById('donate_honor_acknowledgement').value="")) { document.getElementById('fieldset-in_honor').style.display="none"; // this is for hiding document.getElementById('fieldset-in_registration').style.display=""; } else { var answer = confirm("The field you are trying to close will delete all information you entered \"in honor of someone special\" but it will allow you to designate. Clicking the OK button will show the designation list. Do you still want to continue?") if (answer) { document.getElementById('donate_honor_name').value = ""; document.getElementById('donate_honor_acknowledgement').value = ""; document.getElementById('fieldset-in_honor').style.display="none"; // this is for hiding document.getElementById('fieldset-in_registration').style.display=""; } else { document.getElementById('checkmemoriam').checked=false; document.getElementById('fieldset-in_honor').style.display=""; // this is for showing document.getElementById('fieldset-in_registration').style.display="none"; } } } } hiiii, im new to javascripts . Can anyone help me to know that a javascript which is validating a phone number accepts only digits but if the text field is left empty it should accept the entry as an empty entry.... plz share knowledge u will be more knowledgeable Hello. I've been teaching myself HTML and CSS for a while, and now I've moved into the world of Javascript (but I'm still very much a beginner). For practice, I've been building a sample sign up form for a mock website, and I'm having problems with the birthdate section. The idea I had was to have MM, DD, and YYYY be the default values of my 3 textboxes (as an example for users), then set the value to nothing when the box gained focus. That all works fine, but I ran into problems when I tried to write an if statement to set the value back to MM, DD, or YYYY if the value was still nothing when the user clicked away. As it is now it just replaces the text inputted into the textbox (which of course is not good). Any ideas for what the problem might be? Code: <form name="signup" action="javascript:void(0);" method="post"> <table> <tr> <td>Date Of Birth:</td> <td> <input name="DOBmonth" type="text" value="MM" size="2" style="color: #555555;" onFocus="clearDOBmonth()" onBlur="restoreDOBmonth()" /> <input name="DOBday" type="text" value="DD" size="2" style="color: #555555;" onFocus="clearDOBday()" /> <input name="DOByear" type="text" value="YYYY" size="4" style="color: #555555;" onFocus="clearDOByear()" /></td> </tr> <tr> <td></td> <td><button name="Submit" type="submit" style="font-size: 1em;">Sign Up</button></td> </tr> </table> </form> <script type="text/javascript" language="javascript"> <!-- document.signup.DOBmonth.value="MM"; document.signup.DOBday.value="DD"; document.signup.DOByear.value="YYYY"; function clearDOBmonth() { document.signup.DOBmonth.value="";} function clearDOBday() { document.signup.DOBday.value="";} function clearDOByear() { document.signup.DOByear.value="";} function restoreDOBmonth() { if ('document.signup.DOBmonth.value = ""') { document.signup.DOBmonth.value = "MM"; // alert(document.signup.DOBmonth.value); } } //--> </script> Another side question, if I set a variable equal to the value of a textbox, then change the value of the variable, does that also change the value of the textbox? or is the variable no longer associated with the textbox. Example: Code: var a = document.form.textbox.value; a = blue; does document.form.textbox.value = blue? or is var a now completely independent of the textbox value? Thanks! Why isnt this working? Code: function display_form(obj) { var button_name = obj.id.split("dialog_"); var t = document.getElementById(button_name[1] + '_form').style.visibility = 'visible'; alert(t); } IE is saying: Code: id is 'null' or not an object |