...

View Full Version : Can JavaScript toggle a submit button?



FlashDancer
01-20-2012, 08:27 AM
Is it possible for JavaSscript to toggle a submit button?

I will be using it in this code:


<SCRIPT type="text/javascript">
if (document.forms["form"]["quantitys"].value > '.$row['quantity'].') {
alert ("The quantity you wanted for product '.$row['id'].' is no longer available and will be changed to the highest available quantity.");
document.forms["form"]["quantity"].value = '.$row['quantity'].';
// JAVASCRIPT TOGGLE SUBMIT
}
</SCRIPT>


The submit button:
<INPUT name="submit" type="submit" value="UPDATE QUANTITY">

Philip M
01-20-2012, 08:33 AM
If by "toggle" you mean disable, yes.


document.formname.elementname.disabled = true;

You should avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.

FlashDancer
01-20-2012, 08:36 AM
Nah I mean like to activate it, as if the user was to click it.

See how in my code it changes an INPUT value. I need JavaScript then to submit it to the server on the users behalf.


Very interesting. How come I should avoid using function names for id's?

Philip M
01-20-2012, 08:51 AM
Nah I mean like to activate it, as if the user was to click it.

See how in my code it changes an INPUT value. I need JavaScript then to submit it to the server on the users behalf.


Very interesting. How come I should avoid using function names for id's?

Is this what you mean? You will get better answers if you are clearer in your requirements.



<input type = "submit" id = "sub1" value = "Update Quantity" onclick = "doSomething()">
<input type = "button" id = "but1" value = "Click to change Submit" onclick = "changeIt()">

<script type = "text/javascript">
var status = 1;
function changeIt() {
document.getElementById("sub1").value = "Submit the order";
status = 2;
}

function doSomething() {
if (status == 1) {
alert ("The button will update the quantity");
}
else {
alert ("The button will submit the form");
}

}
</script>

It is obviously poor coding practice to use the same name for two different things (form elements/function names/varaible names), but in Internet Explorer, names and id's are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or id. Example:-


<input id = "text">

<script type = "text/javascript">
text = "Hello World"; // global variable
document.getElementById('text').value = text;
</script>

FlashDancer
01-20-2012, 09:01 AM
Thanks for trying to help, but thats not what I wanted either. I just need JavaScript to toggle an existing button on my page. I can't get anymore clearer than that. :)


I don't think its obvious as poor coding practice and I can't see any reason why it shouldn't be done. I use the same names accross HTML, CSS, PHP, JavaScript, SQL and haven't had any problems apart from SQL not liking dashes. It makes it allot easier for me.
In regards to Internet Explorer, targeting browsers when coding is bad practice because it defies web standards.

Philip M
01-20-2012, 09:23 AM
Well, we will have to agree to disagree. :)

Old Pedant
01-20-2012, 09:23 AM
Is it a submit button or an ordinary button?

Does it have an onclick event handler?

If it is a submit button with no onclick event handler, then just do

document.getElementById("idOfTheForm").submit();
If the <form> has an onsubmit handler (e.g. to validate input) then instead do


var form = document.getElementById("idOfTheForm");
if ( nameOfOnSubmitFunction( form ) ) form.submit;


If it is a button with an onclick handler, then just call the function directly. If the onclick handler is passed this as a reference to the button, then


var button = docment.getElementById("theButton");
nameOfOnClickHandlerFunction( button );


And so on.

SHOW CODE.

Old Pedant
01-20-2012, 09:29 AM
I don't think its obvious as poor coding practice and I can't see any reason why it shouldn't be done. I use the same names accross HTML, CSS, PHP, JavaScript, SQL and haven't had any problems apart from SQL not liking dashes. It makes it allot easier for me.
In regards to Internet Explorer, targeting browsers when coding is bad practice because it defies web standards.

JavaScript doesn't like dashes in *names* either.

If you have

<form name="myForm">
<input name="try-this">
then you can *NOT* do


document.myForm.try-this.value = xxx;

The dash is seen as a minus sign. (It's okay in IDs, though frowned on.)

************

As for not having IDs and variable names the same, I used to think the MSIE had fixed the problem. But not so.

*IF* you EXPLICITLY use var someId; then you are okay.

But if you sometime forget to use var, them MSIE will find the element with someId (assuming one exists) and all the things you *THINK* are references to a variable become references to the object. IT IS HIGHLY DANGEROUS to ignore Philip's warning. If you are a really careful coder you can get away with it. But heed the warning.

FlashDancer
01-20-2012, 10:26 AM
I've got an even cooler one that covers them all. :D


document.forms["form"]["submit"].click();

Credit: http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/


Man, JavaScript is so cool!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum