l-emi l-emi - 2 months ago 8
HTML Question

Taking inputted value and putting it on a timer

So I'm making a countdown timer and I'm trying to figure out how to grab the values inputted through the form and put them in the display so the timer can count down from that.

Here's the (probably vastly wrong) JS function I wrote:

function changeTime() {
var h = parseInt(hrs.value);
var m = parseInt(mns.value);
var s = parseInt(scs.value);

if (h < 10) {
h = 0 + h;
};
if (m < 10) {
m = 0 + m;
};
if (s < 10) {
s = 0 + s;
};

$("#time").html() = h + ":" + m + ":" + s;
}

$("#change").click(function() {
changeTime();
})


Here's the form part of the HTML:

<form id="choose">
<input type="text" id="hrs" name="hrs" size="2" maxlength="2" placeholder="h"/>
<input type="text" id="mns" name="mns" size="2" maxlength="2" placeholder="m"/>
<input type="text" id="scs" name="scs" size="2" maxlength="2" placeholder="s"/>
</form>


Here's the whole fiddle. I know the alignment is off, but I'll fix it, I just want help with the JS. Thanks!

Answer

The html() setter should be work only if you added HTML string as the argument in the method.

$("#time").html( h + ":" + m + ":" + s );

Although you need to use string concatenation instead of Number addition.

if (h < 10) {
    h = '0' + h;
    //--^-^--- this should be string 
};
Comments