Guns_Roses
Guns_Roses

Reputation: 165

javascript check if a button has been clicked

i have a problem with a function that is executing once the page is refreshed and onloaded , i want to execute on click event , so there is a button once the user has clicked the button the function should be called and run the code . any ideas ? this is my script

html

<input type="submit" id="fff" value="Valider"  Onclick="validation(zero,one,two,three,four)" >

javascript

function validation(f,a,b,c,d) {
    var str = '';
    var dataString = str;
    $.ajax( {
        type: 'GET',
        url: '/voir/valider/'+f+'/'+a+'/'+b+'/'+c+'/'+d+'/',
        data: dataString,

        success: function(data) {
            $('#shows2').html(data);
        }
    } );
    $('#fff').addClass('hide');
}

Upvotes: 0

Views: 376

Answers (2)

Shanimal
Shanimal

Reputation: 11718

Looks like you might be getting some errors on your parameters, try using strings. The browser probably thinks you have not defined variables of the same name. If your function is being executed automatically it might be that some other code is attempting to call a generic validation function. Try renaming it to Guns_Roses_Validation...

function Guns_Roses_Validation(f,a,b,c,d) {
  console.log("It works!")
}
<input type="submit" id="fff" value="Valider"  onclick="Guns_Roses_Validation('zero','one','two','three','four')" >

Upvotes: 2

Ty Kroll
Ty Kroll

Reputation: 1395

Sounds like you want to only allow a single click to the button.

Add a prop (__clicked, in this case) to the function that is set once and ignored if set.

function validation(h,o,w,d,y) {

  if(!this.__clicked) {
   this.__clicked = true;
   document.body.appendChild(document.createTextNode("clicked"));
  }
 }
<input type="submit" id="fff" value="Valider"  onclick='validation("zero","one","two","three","four")' >

Also put quotes around the args passed in your click handler if they are string.

Upvotes: 0

Related Questions