Question

First of all:
This page is for an online pharmacy.
We are using a small script to retrieve some data from insurance companies to ensure that a user actually has an insurance.

On one of our pages we have 2 text-fields and a button. The text field send a .click() to the button to retrieve some information, but only if they are both filled with the right type of data.
This Works.

To enhance the usability of our site we are creating a flow trough different pages. The page previously described is amongst them. I'm sure that the required information is always in the 2 text-fields so I'm using JavaScript to send a .click() to the button. But somehow $('ZK').click(); does not work, but alert(); $('ZK').click(); does.

Is there any one that is able to explain to me why the first is not working and the second is? And perhaps also how I can fix this minor problem?

Is there something with the .click() not having an event or something?

Code inside this page: (not loaded indirectly)

text-field and button:

<INPUT onkeypress="return noenter()"  
       onkeydown="return isNumericKey(event)"  
 TYPE="TEXT"  
 class="ajaxsearch"  
 name="DOB" id="DOB"  
 value="<%=DOB%>"  
 MAXLENGTH="8"  
 style="width:60px;height:18px"  
 onkeyup="javascript:if ($(DOB).get('value').length==8 &&  
                         $(DOB).get('value')!='ddmmjjjj')
 {  
   var myFx=new Fx.Tween($(DOB));  
   myFx.set('color','#000066');  
   $('ZK').click();  
 } else {  
   var myFx=new Fx.Tween($(DOB));  
   myFx.set('color', '#cc0000');  
 };">  `

<INPUT url="?NC=<%=Rnd(Now)%>"  
 TYPE="BUTTON"  
 NAME="ZK" ID="ZK"  
 VALUE="Zoek mij"  
 class="button"  
 style="width:70px;height:22px">

The call script:

  if ($('DOB').get('value').length==8 
      && $('DOB').get('value')!='ddmmjjjj') 
  {
    var myFx=new Fx.Tween($('DOB'));
    myFx.set('color', '#000066');
    //alert();
    $('ZK').click();
   } else {
     var myFx=new Fx.Tween($('DOB'));
     myFx.set('color', '#cc0000');
   }

Ajax script & call:

 <script language="javascript"> 
    window.addEvent('domready', function() { 
      $('ZK').addEvent('click', function(event) { 
        event.stop(); 
        var req = new Request.HTML({ 
          method: 'post', 
          url: '../ajax/a_bsn.asp',
          data: {
            'BSN':$('BSN').get('value'), 
            'DOB':$('DOB').get('value'),
            'RequestType':'WWW',
            'RXID':'<%=GUID%>'
          }, 
          update: $('RES'),
          onRequest: function() {
            $('RES').innerHTML = '<img width="16px" height="16px" src="../images/spinner.gif"/>';
    }}).send(); 
  });

////////
<%  If (Len(BSN)=8 or Len(BSN)=9) and IsNumeric(BSN) and Len(DOB)=8 and IsNumeric(DOB) Then %>
if ($('BSN').get('value').length>7){
  var myFx=new Fx.Tween($('BSN'));
  myFx.set('color', '#000066');
} else {
  var myFx=new Fx.Tween($('BSN'));
  myFx.set('color', '#cc0000');
}
if ($('DOB').get('value').length==8 && $('DOB').get('value')!='ddmmjjjj'){
  var myFx=new Fx.Tween($('DOB'));
  myFx.set('color', '#000066');
  // var $zk = $('ZK'); console.log($zk); $zk.click();
  // alert('Uw gegevens worden ingevuld.');
  setTimeout("$('ZK').click();",1);
  //$('ZK').click();
} else {
  var myFx=new Fx.Tween($('DOB'));
  myFx.set('color', '#cc0000');
}
<%  End If %>
////////
});
</script>

Div affected by Ajax

<DIV id="RES">      
<INPUT TYPE="TEXT" ID="PNAAM" onkeypress="return noenter()"  NAME="PNAAM" VALUE="<%=PNAAM%>" class="text_inv" style="width:375px">
</div>
Was it helpful?

Solution 4

Our lead-programmer (who originally made the page somewhere around 8 years ago) thought it might be better to move all logic into functions. That way they could be triggered easier in other parts of code if necessary. We also removed all the (old) inline scripts.

In this solution some parts of the other answers were used. I'd like to thank al the other answerers and commenters for their help.

The top part of the code looks like this now (tested and working stable across browsers):

    function updateName(){
        if (document.id('BSN').get('value').length>7 && document.id('DOB').get('value').length==8 && document.id('DOB').get('value')!='ddmmjjjj'){
            var req = new Request.HTML({
                method: 'post',
                url: '../ajax/a_bsn.asp',
                data:{'BSN':document.id('BSN').get('value'),'DOB':document.id('DOB').get('value'),'RequestType':'WWW','RXID':'<%=GUID%>'},
                update: document.id('RES'),
                onRequest: function() {
                    document.id('RES').innerHTML = '<img width="16px" height="16px" src="../images/spinner.gif"/>';
                }
            }).send();
        }
    }
    function checkBSN(){
        if (document.id('BSN').get('value').length>7){
            var myFx=new Fx.Tween(document.id('BSN'));
            myFx.set('color', '#000066');
            updateName();
        } else {
            var myFx=new Fx.Tween(document.id('BSN'));
            myFx.set('color', '#cc0000');
        }
    }
    function checkDOB(){
        if (document.id('DOB').get('value').length==8 && document.id('DOB').get('value')!='ddmmjjjj'){
            var myFx=new Fx.Tween(document.id('DOB'));
            myFx.set('color', '#000066');
            updateName();
        } else {
            var myFx=new Fx.Tween(document.id('DOB'));
            myFx.set('color', '#cc0000');
        }
    }
    window.addEvent('domready', function() {
        document.id('ZK').addEvent('click', function(event) {
            event.stop && event.stop();
            updateName();
        });
        document.id('BSN').addEvent('keyup', function(event) {
            event.stop && event.stop();
            updateName();
        });
        document.id('DOB').addEvent('keyup', function(event) {
            event.stop && event.stop();
            updateName();
        });
    <%  If (Len(BSN)=8 or Len(BSN)=9) and IsNumeric(BSN) and Len(DOB)=8 and IsNumeric(DOB) Then %>
        updateName();
    <%  End If %>
    });

OTHER TIPS

Most likely, among other errors, you have a previous statement which is not ended with a semicolon. Try removing the alert() and leaving the ; to test. Also, if you haven't, check your console as you should see at least 1 error.

After your edits my initial answer stopped making sense.

EDIT :

Your click handler and ajax call are wrapped in a domready event handler function. Your "call script" should also be in domready. According to your latest edits the domready handler is closed just before the ASP tag <%

This is what you posted:

        }).send();  // --this is the end of ajax
    }); // -- this is the end of the click handler
}); // -- this is the end of domready
<%  If (Len(BSN)=8

So your call script:

if ($('DOB').get('value').length==8 && $('DOB').get('value')!='ddmmjjjj') {
    var myFx=new Fx.Tween($('DOB')); myFx.set('color', '#000066');
    //alert();
    $('ZK').click();
} else { var myFx=new Fx.Tween($('DOB')); myFx.set('color', '#cc0000'); }

needs to go inside domready after you have attached the click handler:

window.addEvent("domready", function(){
    // Let's attach the click handler:
    $('ZK').addEvent('click', function(event) {...do the ajax here etc...});

   // the "call script" needs to be here

});

You need to wait for the document structure to load.

By the way, it would help if you stopped moving the code around in your question - just post what you are currently using.

reuse your functions, selectors and variables...

window.addEvent('domready', function() {

    var ZK = $('ZK');
    ZK.addEvent('click', function(event) { 
        // make event safe and reusable via software call
        event.stop && event.stop(); // don't call stop if not a real event.

        // logic here


    });

    $('DOB').addEvent('keyup', function() {
        // this == DOB
        var value = this.get('value'); // get it once.

        if (value.length && value.length === 8 && value !== 'ddmmjjjj') {
            // do not call click! we can now fireEvent
            ZK.fireEvent('click'); 
        }
    });
});

don't use inline js. don't rely on $, use document.id instead for mootools 1.2.4+

to recap, do not go to the dom to do a synthetic .click() - its not the most reliable and you don't need to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top