2

I am working on getting the user t input a number for something in this case it would be a resistor value, I have asked them via a prompt to pick a number and then if that number is within the limits I have set then the number would be displayed back to the user within a div.

I have built this already and got it to work with a couple of test messages so I believe the function itself is fine however I am having a problem that whenever the user enters a correct value that value isn't displayed but "undefined" is displayed instead.

This is the HTML I am testing,

<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>

And this is the JavaScript function

function R1Value() {
var R1ValueEntered = prompt("please enter a value for R1:")
var R1 = parseInt(R1ValueEntered)
var display = document.getElementById('test3');

if (R1 >= 120 && R1 <= 1000000) {
    display.innerHTML = R1.value;
} 
else {
    alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
    }
} 

I have placed into into a jsfiddle as even though there isn't a lot of code it may save you some time if you can have a look, http://jsfiddle.net/2ufnK/72/ I may be missing something simple but I can't seem to fix the problem.

2
  • 1
    display.innerHTML = R1.value; Integers don't have a value property. Commented Apr 21, 2015 at 16:34
  • @zzzzBov Ah thank you so much, i'm sorry it was a silly question but i just couldn't see it, well I know for next time thanks again. Have a nice day! Commented Apr 21, 2015 at 16:36

6 Answers 6

5

Just remove .value :

if (R1 >= 120 && R1 <= 1000000) {
    display.innerHTML = R1;
} 

FIDDLE

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

4 Comments

@R3tep Thank you for this I am sorry I don't have enough rep to upvote your reply, unless i am doing that wrong too! But thank you for this I am sorry it was so simple! Have a nice day!
@A.J., you don't need to downvote an answer just because you beat it to seconds. The user was probably typing at the same time than you and it happened that you were faster by seconds. "Already answered" and downvote would fit better if the user had answered like 10 minutes later. The same goes for your comment on the answer of sdla4ever
@Liam Validate the answer is perfect ;)
@R3tep Of course thank you for the help! And I just earned enough rep to upvote, enjoy! :)
4

.value is undefined for integers. Remove that and your code will work fine.

Comments

3

You don't need the R1.value. Just calling R1 will return it's value.

3 Comments

@Andy, yes but this exact information is contained in my answer, thus it is of no help to anyone.
@A.J. I didn't hit refresh before typing my answer so your answer was not there.
Either way thank you all for the answers they were all appreciated, have a nice day all of you :)
1

you got an error here, you assign R1 value, not R1.value

if (R1 >= 120 && R1 <= 1000000) {
  display.innerHTML = R1;
}

Comments

1

I tried your code. Besides that you might have missed a closing tag, it worked for me. You need to change these lines:

    if (R1 >= 120 && R1 <= 1000000) {
    display.innerHTML = R1.value;
} else {
    alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}

to:

    if (R1 >= 120 && R1 <= 1000000) {
    display.innerHTML = R1;
} else {
    alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
}

you don#t need to get the value of R1, because it already IS hte Value.

I hope i could help. regards

Comments

-1

Your problem is that you are never closing your <div> and you also call R1.value, which is basically calling .value on an integer, which is undefined. Try the following:

function R1Value() {
    var R1ValueEntered = prompt("please enter a value for R1:")
    var R1 = parseInt(R1ValueEntered)
    var display = document.getElementById('test3');

    if (R1 >= 120 && R1 <= 1000000) {
        display.innerHTML = R1;
    } else {
        alert("That is an incorrect value please enter one between 120Ohms and 1MOhm.");
    }
}
<button onclick="R1Value()">Change Value</button>
<div id="test3"></div>

2 Comments

Thank you for this I am sorry it was a simple error on my part, but thank you for the help. Have a nice day! @A.J.
@Liam if this answer helped you, would you mind accepting it? Thanks!

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.