html form text elements, not be parsed correctly into javascript method

html form text elements, not be parsed correctly into javascript method



I have a simple calculate.js function, that calculates a price and displays it to the user. Here is my function:


function calculatePrice()

//Get selected data
var width = document.getElementById("width");

var heigth = document.getElementById("height");

var elt = document.getElementById("style");
var style = elt.options[elt.selectedIndex].value;

elt = document.getElementById("materials");
var materials = elt.options[elt.selectedIndex].value;

elt = document.getElementById("priority");
priority= elt.options[elt.selectedIndex].value;

//convert data to integers
style = parseInt(style);
materials = parseInt(materials);
priority = parseInt(priority);
width = parseInt(width);
height = parseInt(height);

//calculate total value
var total = 500* (height + width) + (style + materials + priority)

//print value to PicExtPrice
document.getElementById("PicExtPrice").value=total;



Everything was working fine until I started using the width and height variables. The issue is that they are text fields, and I want the user to
input a number directly, instead of selecting an option like the other values.



HTML code for input fields:


<div class="col-lg-6">
Indæst bredden på ønskede malleri
<input name="width" type="text" maxlength="512" id="width" onChange="calculatePrice()"/>
</div>

<div class="col-lg-6">
Indæst længden på ønskede malleri
<input name="heigth" type="text" maxlength="512" id="heigth" onChange="calculatePrice()"/>
</div>



example of an dropdown option:


<div class="col-lg-6">
<SELECT NAME="Stil" onChange="calculatePrice()" id="style">
<OPTION value="0">Hvilken stil skal malleriet være mallet i</OPTION>
<OPTION value="1">Kubisme</OPTION>
<OPTION value="2">Impressionisme</OPTION>
<OPTION value="3">Pop/Ekspressionisme</OPTION>
</SELECT>
</div>





Hello bailey. you should set type of your weight and height inputs to numbers type="number" . in this way user can only type number in it and in javascript you should parseInt(x) the value you get from that element.
– Masoud Amidi
Aug 26 at 9:27





i have tried this, but it did not help
– baileyhaldwin
Aug 26 at 9:28





var width = document.getElementById("width"); should be var width = document.getElementById("width").value;. Same goes with height.
– asynts
Aug 26 at 10:42



var width = document.getElementById("width");


var width = document.getElementById("width").value;


height





You had forgotten a semicolon to terminate the line that performs the computation. Expect JavaScript to hiccup in this case.
– Robidu
Aug 26 at 11:37




4 Answers
4



You have some incorrect spelling on your inputs Id and in javascript, please check and correct them.



Change your javascript function to this and your problem is should be solved.


function calculatePrice()

//Get selected data
var width = document.getElementById("width").value;

var height = document.getElementById("height").value;

var elt = document.getElementById("style");
var style = elt.options[elt.selectedIndex].value;

elt = document.getElementById("materials");
var materials = elt.options[elt.selectedIndex].value;

elt = document.getElementById("priority");
priority = elt.options[elt.selectedIndex].value;

//convert data to integers
style = parseInt(style);
materials = parseInt(materials);
priority = parseInt(priority);
width_int = width == "" ? 0 : parseInt(width);
height_int = height == "" ? 0 : parseInt(height);

//calculate total value
var total = 500 * (width_int + height_int) + (style + materials + priority);

//print value to PicExtPrice
document.getElementById("PicExtPrice").value = total;




The problem was you should first get value of element. second you should check it if there is no value set it to zero.





I agree with this answer but you're missing a semicolon at the end of this line (not sure if it would make a difference but maybe good to correct it) var total = 500 * (width_int + height_int) + (style + materials + priority);
– Sarah
Aug 26 at 9:54





thanks Sarah. I did correct it but it doesnt make problem when I tested it.
– Masoud Amidi
Aug 26 at 9:56





@MasmoudAmidi That's because javascript autofills semi-colons to prevent unnecessary errors. However it still is a good practice to use them nonetheless
– Rawrplus
Aug 26 at 10:04






@rawrplus Ok that's good. I was just following good practice :) I tested this answer too and it works :)
– Sarah
Aug 26 at 10:11






Perhaps a good thing to note, this would throw an error if you used strict mode via "use strict"; or even used any common module programming model.
– Rawrplus
Aug 26 at 10:12


"use strict";



If I understand your problem correctly, you are parsing the input object instead of the value of the input.



You can take any value from an object that was retrieved with getElementById using the value getter.


getElementById


var height = document.getElementById("heigth");
height = parseInt(height.value); //instead of height = parseInt(height); parse the value, not the object



Use it on both height and width and you will work with the values that the user has provided.





this did not work, it did not display any value...
– baileyhaldwin
Aug 26 at 9:28



in the var height line you have a typo. You have typed heigth.





i have fixed this now
– baileyhaldwin
Aug 26 at 9:26





in input's name also you have the same spelling!. And You haven't corrected. HEIGHT.. make corrections in all places
– Padmanabhan
Aug 26 at 9:31






@baileyhaldwin You also have id="heigth" instead of id="height" in the HTML <input> element
– Sarah
Aug 26 at 9:36





whatever spelling you use, make it same everywhere.
– Padmanabhan
Aug 26 at 9:37





@baileyhaldwin Also Oscar is right in what he said below. you need to get the value of the input field (not just the input field itself) so something like var height = document.getElementById("height").value; (note the .value on the end).
– Sarah
Aug 26 at 9:41



I have derived a test case from your descriptions, and after a bit of tinkering I got it to work as it should (it actually computes a result from the input). However, I have reworked things a bit.



Keyword "Unobtrusive JavaScript": I'm not hard-coding any event handlers into the (X)HTML, but instead I'm waiting for the DOMContentLoaded event to fire and use it to attach event listeners to each individual input field (this way I am guaranteed to have the entire DOM tree set up).



Furthermore I have trapped all values by attempting to convert everything to integers and subsequently also check for the values being a NaN - in the latter case your calculation would produce an invalid result and cause your JavaScript to abort. However, when NaNs are trapped and reset to zeroes, the computation works just fine.



As a last hint, please scan your code and check for any typos - misspelled IDs and the likes that could cause getElementById to return null or undefined.


getElementById



As a final measure you can also unclutter your code by avoiding indexing into the selections, but instead retrieve their values directly (yes, document.getElementById('style').value works!). I successfully checked that in my test case.


document.getElementById('style').value






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế