最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display an integer into a div - Stack Overflow

programmeradmin3浏览0评论

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, / I may be missing something simple but I can't seem to fix the problem.

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/2ufnK/72/ I may be missing something simple but I can't seem to fix the problem.

Share Improve this question edited Apr 21, 2015 at 16:35 j08691 208k32 gold badges269 silver badges280 bronze badges asked Apr 21, 2015 at 16:31 LiamLiam 1393 silver badges13 bronze badges 2
  • 1 display.innerHTML = R1.value; Integers don't have a value property. – zzzzBov 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! – Liam Commented Apr 21, 2015 at 16:36
Add a ment  | 

6 Answers 6

Reset to default 5

Just remove .value :

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

FIDDLE

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

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

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

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

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

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>

发布评论

评论列表(0)

  1. 暂无评论