Jump to content

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;
    }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.