We help IT Professionals succeed at work.

Javascript - How can I trigger a button click when a text field has data inputed to it?

circle1d
circle1d asked
on
This should be pretty simple, I just can't get it. I need a ClickMe command of some type to automatically press a button that is being manually pressed now. The user fields in his name and then presses a button to populate the email, address and phone number fields, I need that button press to be transparent of the user.
Comment
Watch Question

Yep, this is an easy one.  Just call the button's click method:

document.formId.buttonId.click();

Author

Commented:
The text field is named "CallerName", when the field is focused or changed on exited, I need th e"go" button to be pressed. I hate being such a novice, but please provide the code and the placement thereof to get this to function properly so that th euser does not have to press it, nor even see the button
Are you sure that you want the button clicked when the field is focused AND when the field value changes?  Normally, you would just click the button when the field value changes.

Either way, it's no problem.  I'll show how to click the button only when the field value changes.  To make it click the button when the field is focused AND when the field value changes, you just need to uncomment one line of code in callerNameOnFocus.

What we are going to do is to save the current field value when the field is focused, then check the value and click the button when the field is exited.  To do this, you'll need to setup 2 functions that will act as event handlers, then just add the event handlers to the <input> tag.

The 2 event handlers will be named callerNameOnFocus and callerNameOnBlur:


      <head>
      <script type="text/javascript">
      
      var savedCallerName = "";
      
      function callerNameOnFocus(){
            savedCallerName = document.forms[0].CallerName.value;
            // document.forms[0].go.click(); // uncomment this line if you really want to click the button each timethat the field is entered.
      };
      
      function callerNameOnBlur(){
            if (document.forms[0].CallerName.value != savedCallerName){
                  document.forms[0].go.click();
            };
            savedCallerName = document.forms[0].CallerName.value;
      };
      
      </script>
      </head>
      

Now, just add the event handlers to your <input> tag:


      <input type="text" id="CallerName" onfocus="callerNameOnFocus()" onblur="callerNameOnBlur()">


That's all there is to it!

Author

Commented:
What I am doing is utilizing a Domino Name Picker, located here... http://www.codestore.net/store.nsf/unid/FISR-6MPRGU to pick a caller's name from a names.nsf drop down list, once the name has been inputed into the callername field, I click the "go" button to run a DBLOOKUP on that name to populate 3 other fields, email, phone and company. Of course the domino name picker is far to complex for me to figure out what I need to do in order to get the script to populate all the fields without having to run a separate DBLookup for them. I will be accepting your answer tomorrow, but I am giving you the opportunity to look at the code to see if you would like to tackle my issue from the name picker standpoint and if so I will tack on an additional 375 points. Thanks
Small world!  I'm a certified Domino developer AND I've implemented a similar name picker in my own web apps.

There are a couple of things that you can do to make your app much cleaner, but I don't have time to explain right now.

One method involves calling a function from the popup instead of setting a field value.

The other method involves calling @DbLookup from JavaScript via AJAX.

Neither technique is very complicated, and will eliminate the button-click workaround.

Author

Commented:
I gave you a B because you said you had more to offer, but didn't have time to share it, so an A would not be appropriate, thanks.
I really don't care about the points that much, but this time, you struck a nerve!

You should not have reduced the grade since the original question (How can I trigger a button click?) was answered clearly and correctly.

I WAS going to assist you with the Domino-specific aspects so that your picker control would really shine.  Now I just don't feel like it.

Author

Commented:
I have no qualms about your not being able to assist me further, I appreciate your assistance. This is my first time awarding points and only lowered the grade because you said you could have done more. There was no intent to offend and I hope that this experience will not cause you to pass by my name if you should see a question from me in the future. Thanks.
No problem, you probably never read the grading guidelines:

"An answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade."

Explore More ContentExplore courses, solutions, and other research materials related to this topic.