Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Banned
    Join Date
    Jan 2012
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can JavaScript toggle a submit button?

    Is it possible for JavaSscript to toggle a submit button?

    I will be using it in this code:
    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">

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If by "toggle" you mean disable, yes.

    Code:
    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'.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Banned
    Join Date
    Jan 2012
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?
    Last edited by FlashDancer; 01-20-2012 at 07:39 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by FlashDancer View Post
    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.

    Code:
    <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:-

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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Jan 2012
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.
    Last edited by FlashDancer; 01-20-2012 at 08:10 AM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, we will have to agree to disagree.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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
    Code:
    document.getElementById("idOfTheForm").submit();
    If the <form> has an onsubmit handler (e.g. to validate input) then instead do
    Code:
    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
    Code:
    var button = docment.getElementById("theButton");
    nameOfOnClickHandlerFunction( button );
    And so on.

    SHOW CODE.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    FlashDancer (01-20-2012)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Quote Originally Posted by FlashDancer View Post
    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
    Code:
    <form name="myForm">
        <input name="try-this">
    then you can *NOT* do
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    FlashDancer (01-20-2012)

  • #9
    Banned
    Join Date
    Jan 2012
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I've got an even cooler one that covers them all.

    Code:
    document.forms["form"]["submit"].click();
    Credit: http://www.randomsnippets.com/2008/0...ia-javascript/


    Man, JavaScript is so cool!
    Last edited by FlashDancer; 01-20-2012 at 09:34 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •