Results 1 to 5 of 5

Thread: Using onchange in HTML

  1. #1
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Using onchange in HTML

    In the below HTML code I have a onchange="myFunction(event)" and I have a function called myFunction

    My first question is what does the word event mean in the function call and my second question is what is the argument that I need in the function argument list.

    Code:
    <html>
    <body>
    
    <input type="text" name="MyTextbox" size="40" value="" maxlength="160" onchange="myFunction(event)">
    
    <script language=javascript>
    function myFunction(what_goes_here?)
    {
      //
      // how to process the argument?
      //
    }
    </script>
    
    </body>
    </html>
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 08
    Location
    Trivandrum, Kerala, India
    Posts
    7,557

    Re: Using onchange in HTML

    onChange is the event here. That is when the text in the inputbox is changed, the onChange event is fired.

    If you pass "this" as parameter, then it refers to that element itself. Then you can access it in the function.

    Here's an example:
    HTML Code:
    <html>
    <body>
    
        <input type="text" name="MyTextbox" size="40" value="" maxlength="160" onchange="myFunction(this)" />
    
    <script language="javascript">
    function myFunction(ele)
    {
      alert(ele.value);
    }
    </script>
    
    </body>
    </html>
    To test it live: http://jsfiddle.net/wrFUD/
    Type something in the box, and click somewhere outside the box. You'll get an alert box with the content of the inputbox.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD Athlon X2 5200+, ASUS Motherboard, 2 GB RAM, 400 GB HDD, Nvidia 8600 GT 512MB, 19.5" TFT(Wide), Creative 5.1 Home Theater

    Social Group: VBForums - Developers from India

    Skills: PHP, MySQL, jQuery, VB.Net, VB6, Photoshop...

  3. #3
    PowerPoster
    Join Date
    Jan 08
    Posts
    6,780

    Re: Using onchange in HTML

    I already know about passing this but that is not what my problem is. The call passes event. That is what I am dealing with not this .
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved


    When I was young and in my prime I used to program all the time but now I'm old and getting gray I only program once a day

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 08
    Location
    Trivandrum, Kerala, India
    Posts
    7,557

    Re: Using onchange in HTML

    I believe it addresses that event (in your case the change event).

    You could check that by alerting variablename.type

    Example:
    Code:
    <input type="text" name="MyTextbox" size="40" value="" maxlength="160" onchange="myFunction(event)" />
    
    <script language="javascript">
    function myFunction(ele)
    {
      alert(ele.type);
    }
    </script>

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD Athlon X2 5200+, ASUS Motherboard, 2 GB RAM, 400 GB HDD, Nvidia 8600 GT 512MB, 19.5" TFT(Wide), Creative 5.1 Home Theater

    Social Group: VBForums - Developers from India

    Skills: PHP, MySQL, jQuery, VB.Net, VB6, Photoshop...

  5. #5
    New Member
    Join Date
    Dec 12
    Location
    United Kingdom
    Posts
    3

    Re: Using onchange in HTML

    The onChange happens whenever anything changes the value of the field, not just when the user enters a value. If a field's onChange changes the value of the field, it's easy to get an endless loop. In situations like this, be sure to check if the change needs to happen. For example, this product order form makes sure that the "total" field is always the correct total (even if the user changes it). The subtotal field "vn_stVis" uses onChange to check if it is different than the hidden subtotal field "vn_stHold". If there is a difference, vn_stVis is reset, which then causes another onChange event. However, that second time around the two fields are the same, and the script ends.

Posting Permissions

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