/    Sign up×
Community /Pin to ProfileBookmark

Help js return nan as result

hi to all i am trying to make lil js calculator but i all time get nan result ?
Try no 1?

[CODE]<div class=”calculator”> <!–calculator test–>
<div id =a><?php echo $Data->var_price1; ?></div>
<div id =c><?php echo $Data->var_price2; ?></div>
<input id=”odrasli” type=”number”>
<input id=”deca” type=”number”>
<input type=”button” value=”Get Price” onClick=”mycalculator()”>
<script>
function mycalculator() {
var a = document.getElementById(“a”);
var x = document.getElementById(“odrasli”);
var c = document.getElementById(“c”);
var y = document.getElementById(“deca”);
var aa = a * x;
var cc = c * y;
var p = aa + cc;
var d = p * 0.2;
document.getElementById(“price”).innerHTML = p;
document.getElementById(“depozit”).innerHTML = d;
}
</script>
</p> Grand total </br><p id=”price”> </br>
Deposite<p id=”depozit”></p> </br>

</div>[/CODE]

Try no 2 still nan

[CODE]<div class=”calculator”> <!–calculator test–>
<div id =a><?php echo $Data->var_price1; ?></div>
<div id =c><?php echo $Data->var_price2; ?></div>
<input id=”odrasli” type=”number”>
<input id=”deca” type=”number”>
<input type=”button” value=”Get Price” onClick=”mycalculator()”>
<script>
function mycalculator() {
var a = document.getElementById(“a”);
var x = document.getElementById(“odrasli”);
var c = document.getElementById(“c”);
var y = document.getElementById(“deca”);
var aa = a * x;
var cc = c * y;
var p = aa + cc;
var d = p * 0.2;
document.getElementById(“price”).innerHTML = p;
document.getElementById(“depozit”).innerHTML = d;
}
</script>
</p> Grand total </br><p id=”price”> </br>
Deposite<p id=”depozit”></p> </br>

</div>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TcobbMay 25.2015 — When you use "document.getElementById" it returns a reference to the HTML element rather than what it contains, which is the number value you want.

Instead of
[CODE]var a = document.getElementById("a");[/CODE]

try

[CODE]var a = document.getElementById("a").innerHTML;[/CODE]
Copy linkTweet thisAlerts:
@aleksa1302authorMay 25.2015 — [CODE]
<div class="calculator" style="position:left;"> <!--calculator test-->
<div id =a style='display:none;'><?php echo $Data->var_price1; ?></div>
<div id =c style='display:none;'><?php echo $Data->var_price2; ?></div>
<input id="odrasli" type="number" placeholder="Number of Adults">
<input id="deca" type="number" placeholder="Number of Children's">
</br>
<button value="Submit" class="submite_btn" title="Submit" type="submit" onClick="mycalculator()"><span class="">Get Price</span></button>
<!-- <input type="button" class= "submite_btn" value="Get Price" onClick="mycalculator()">
-->
<script>
function mycalculator() {
var a = parseInt(document.getElementById("a").innerHTML); <!-- eve problema :) parseInt -->
var x = parseInt(document.getElementById("odrasli").value);<!-- eve problema :) parseInt i nije .innerHTML vec .value -->
var c = parseInt(document.getElementById("c").innerHTML);<!-- eve problema :) parseInt -->
var y = parseInt(document.getElementById("deca").value); <!-- eve problema :) parseInt i nije .innerHTML vec .value -->
var aa = a * x;
var cc = c * y;
var p = aa + cc;
var d = p * 0.2;
document.getElementById("price").innerHTML = p;
document.getElementById("depozit").innerHTML = d;
}
</script>
<div class = "calcres"style="position:right;">
</br>
Grand total</br>
<div id="price"></div> <div class="dolar">$</div></br>
Deposite </br>
<div id="depozit"></div> <div class="dolar">$</div></br>
</div>
</div>
[/CODE]

fixed
×

Success!

Help @aleksa1302 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...