zang8027 Posted May 14, 2009 Share Posted May 14, 2009 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 More sharing options...
zang8027 Posted May 14, 2009 Author Share Posted May 14, 2009 forgot to mention "quan" is the input box i have and the if statement works.. Link to comment https://forums.phpfreaks.com/topic/158150-setting-a-variable-for-input/#findComment-834228 Share on other sites More sharing options...
Psycho Posted May 14, 2009 Share Posted May 14, 2009 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; } } Link to comment https://forums.phpfreaks.com/topic/158150-setting-a-variable-for-input/#findComment-834277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.