0

I do not use jQuery, unless this will force me.

My apparently simple HTML document has a div with 3 inputs, the 3rd of which, for some reason, I fail to reset. My cleaned-up-for-display code appears below:

<div>
 <label>LIMIT 1
  <select id="limit1"> 
   <option value="1">Choice1</option>
   <option value="2">Choice2</option>
  </select>
 </label>
 <label>LIMIT 2
  <select id="limit2">
   <option value="1">Choice1</option>
   <option value="2">Choice2</option>
  </select>
 </label>

 <label>ENTER A VALUE
  <input type="text" id=“parm2” value="20" size="3" maxlength="4">
 </label>
</div>

Here’s my validation and reset code:

var limit1 = document.getElementById("limit1");
var limit2 = document.getElementById("limit2");
if (limit2.value < limit1.value) {
 alert("ERROR: 1st must be less than 2nd");
 limit1.value = 1; 
 limit2.value = 2;
 return false;
}  // Above resets of select options works!
var uValue = document.getElementById(“parm2”).value;
if (isNaN(uValue)) {
 uValue.value = "20";   // This reset fails!
 return false;
}

Thanks for your help.

2 Answers 2

1
var uValue = document.getElementById(“parm2”).value;

selects the value of the element, not the element. so "123".value doesn't really make sense, store the element if you want to reset its value.

var uElement = document.getElementById('parm2');
// later on
uElement.value = 20;

Side Note: property access usually isn't expensive, so storing just the element and accessing the value with .value later on is perfectly acceptable instead of using two variables.

Sign up to request clarification or add additional context in comments.

3 Comments

$('#beard').grow(); wasn't working so I had to GIMP it.
@ruffy did you ever solve your issue? If so please update us and/or accept an answer.
My problem was as BOTH RESPONDERS pointed out, namely, that I had accessed only a value - instead of a node. AFTER I have identified the node, THAT's when I can alter its value. In my original question, I had the 3rd pegged wrong; I should have accesses it like I did the former two. Now that it was pointed out to me, I was embarrassed in hindsight, for now it's obvious. I have checked on both RESPONDERS' up arrows, but only one of them "takes" - but I want to thank BOTH RESPONDERS.
0

You're accessing .value twice

var parm2 = document.getElementById(“parm2”);
if (isNaN(parm2.value)) {
 parm2.value = "20";   // This reset no longer fails!
 return false;
}

3 Comments

might be more useful to explain the changes you've made for the OP.
Thanks tcigrand. (What's OP stand for?)
Original Poster. aka, you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.