Raidmc
Raidmc

Reputation: 1

Making dropdown unit converter but cant get it to change when i select option

I am trying to make my function switch when I select a different converter. Right now I just have one set so I can learn how to do it right then I will add the others. This is what I have right now

Html

<title>Conversion Page</title>
<h1>Conversion</h1>
<p>
<input type="text" id="input" />
<select id="mySelect"       onchange="this.options[this.selectedIndex].value;">
<option value="k_to_lbs">Kg's to Lb's"</option>
<option>something</option>
</select>
<button id="submit">Submit</button>
<text id="result"></text>
</p>

Javascript

var reportpounds = function (pounds) {
document.getElementById("result").innerHTML = pounds + "lbs"
}
if (document.getElementById("mySelect")[0] === true) {
    document.getElementById("submit").onclick = function () {
    var k = document.getElementById("input").value;
    reportpounds(Math.round(k * 2.2046226218));
}
} else {
document.getElementById("submit").onclick = function () {
    var k = document.getElementById("input").value;
    reportpounds(k);
}
}

http://jsfiddle.net/mwinberry/2fymfk35/

the Issue I am having is making it run the proper function when I select a specific item from my drop down list.

any help would be greatly appreciated

Upvotes: 0

Views: 134

Answers (1)

beautifulcoder
beautifulcoder

Reputation: 11340

Your if is in the wrong place:

document.getElementById('submit').onclick = function () {
    var k = document.getElementById('input').value, 
        f = 1;
    if (document.getElementById('mySelect').value === 'k_to_lbs') {
        f = 2.2046226218;
    }
    reportpounds(Math.round(k * f));
};

It needs to go inside the onclick callback.

Upvotes: 1

Related Questions