Skip to content Skip to sidebar Skip to footer

Passing Html Values Into Javascript Functions

I was making a javascript function in which I need to confirm the input. I wrote the following code but its giving negative value i.e. 'else' part even if i enter a valid value. Ca

Solution 1:

Give the textbox an id of "txtValue" and change the input button declaration to the following:

<inputtype="button" value="submit" onclick="verifyorder(document.getElementById('txtValue').value)" />

Solution 2:

Here is the JSfiddle Demo

I changed your HTML and give your input textfield an id of value. I removed the passed param for your verifyorder function, and instead grab the content of your textfield by using document.getElementById(); then i convert the str into value with +order so you can check if it's greater than zero:

<input type="text" maxlength="3" name="value" id='value' />
<inputtype="button"value="submit"onclick="verifyorder()" />
</p>
<pid="error"></p><pid="detspace"></p>functionverifyorder() {
        var order = document.getElementById('value').value;
        if (+order > 0) {
            alert(+order);
            returntrue;
        }
        else {
            alert("Sorry, you need to enter a positive integer value, try again");
            document.getElementById('error').innerHTML = "Sorry, you need to enter a positive integer value, try again";
        }
    }

Solution 3:

Simply put id attribute in your input text field -

<inputtype="text" maxlength="3" name="value"id="value" />

Solution 4:

Try: if(parseInt(order)>0){....

Post a Comment for "Passing Html Values Into Javascript Functions"