www.webdeveloper.com
Results 1 to 6 of 6

Thread: OnClick and OnChange with same element

  1. #1
    Join Date
    Dec 2002
    Location
    Warwickshire, England
    Posts
    182

    OnClick and OnChange with same element

    Hi all,

    I have a small HTML form that has a textfeild which when clicked on open a calender, this works fine. I also want to display the selected date in another textfeild using "OnChange". This is what I am working with:

    TEXTFEILD
    Code:
    <input name="startDate" type="text" class="bodytext" id="startDate" size="10" maxlength="10" onclick='scwShow(this,this);' onchange="PrintValues3();"  target="_parent._top" onmouseclick="this.focus()">
    The "OnChange" call a small script:
    Code:
    function PrintValues2(){
    
      var myString = document.form1.startDate.value
      var myStringLength = myString.length
      var Comma = myString.lastIndexOf(',')
      var SufNumChars = Comma + 1
    
      document.form1.result2.value=('');
    
      for(i=0; i<Comma; i++) 
      document.form1.result2.value+=(myString.charAt(i));
    
      document.form1.result2.value+=(' ');
    
      for(i=SufNumChars; i<myStringLength; i++) 
      document.form1.result2.value+=(myString.charAt(i));
    }
    The result2 is then displayed in a textfeild called "result2" but in my case its not.

    Is it possible to use both "OnClick" and "Onchange" together?

    Any help would be great.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Question

    Have you tried putting both actions into a single onclick event?
    Code:
    <input name="startDate" type="text" class="bodytext" id="startDate" size="10" maxlength="10"
     onclick="scwShow(this,this);PrintValues3();"  target="_parent._top" onmouseclick="this.focus()">

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    Your function is PrintValue2 or PrintValue3 ? Then it's easier to replace a comma by a space with the replace method...
    Code:
    document.form1.result2.value=document.startDate.value.replace(',',' ');

  4. #4
    Join Date
    Dec 2002
    Location
    Warwickshire, England
    Posts
    182
    Hi 007Julien and JMRKER,

    I code both approaches but they did not work.

    This line should have read:
    Code:
    <input name="startDate" type="text" class="bodytext" id="startDate" size="10" maxlength="10" onclick='scwShow(this,this);' onchange="PrintValues2();"  target="_parent._top" onmouseclick="this.focus()">
    PrintValues2(); not PrintValues3();

    check that all the vars are correct but failure, any ideas?

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Question

    Try changing 'onclick' to 'onblur'.
    That way actions do not start until field is exited, after changing all information of the field.

    Still a bit unclear as to what you are trying to do.
    Can you describe actions and desires you wish to accomplish?

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I think that:

    1. You should replace onclick with onfocus
    Thus the function will fire even if someone uses the TAB to circle through the controls

    2. You should use onblur instead of onchange, if you want to fire that function whenever the control looses the focus.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles