Jump to content

setting a variable for input


zang8027

Recommended Posts

is there a way to set a variable inside html quotes?

 

<form onsubmit="validateIt()">
I want like <input type='text' name='amount_1' value='javascript variable' />

 

and on submit of the form, i want to set that variable in a function

 

function validateIt()
{
var quan= document.getElementById('fname');
var amount = document.getElementByName('amount_1');
var howMany = document.getElementByName('quantity_1');

if(quan.value>=0)
{
if(quan.value<=1000)
{
	amount.value="0.19";
	howMany.value=quan.value;

}
} 

if(quan.value>=1001)
{
if(quan.value<=5000)
{
	amount.value="0.18";
	howMany.value=quan.value;

}
} 

if(quan.value>=5001)
{
if(quan.value<=10000)
{
	amount.value="0.17";
	howMany.value=quan.value;

}
}
}
</script>

 

 

Link to comment
https://forums.phpfreaks.com/topic/158150-setting-a-variable-for-input/
Share on other sites

Why do you need to set the value between the quote marks? You could only do that during the page load with a write() command. Makes much more sense to just set the value of the object as your function is doing above. Is that function not working? In any event it is inefficient.

 

function validateIt()
{
    var quan= document.getElementById('fname');
    var amount = document.getElementByName('amount_1');
    var howMany = document.getElementByName('quantity_1');

    if(quan.value>=0 && quan.value<=1000)
    {
        amount.value="0.19";
        howMany.value=quan.value;
    } 
    else if(quan.value>1000 && quan.value<=5000)
    {
        amount.value="0.18";
        howMany.value=quan.value;
    } 
    else if(quan.value>5000 && quan.value<=10000)
    {
        amount.value="0.17";
        howMany.value=quan.value;
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.