2

Hi all im very new to javascript so please be gentle. im mixing php with my calls and I have a slight issue. I need to alter the function name that is called in an onclick event.

<div class=\"span4\" id=\"pass\">
    <button class=\"btn btn-block btn-large btn-warning\" id=\"bypass\" disabled onclick=\"pass(); return false;\">
        - 
    </button>
</div>

above is the div with the std call. before this point some variables are set from another function call and I need to change the above call to "pinpass2() or pinpass3 etc.

function pincheck(id,type,lastid,pin){
    document.getElementById('fade').style.display=\"block\";
    document.getElementById('calc').style.display=\"block\";  
    var staffid = id;
    document.getElementById('bypass').onclick = function (){\"pinpass\"+staffid(); 
    return false;
};
}

the above is the function that should do it but i can't seem to get it working. Any help appreciated. p.s if i include the following into the pincheck function the desired staffid is diaplayed

alert(\"staff id\"+staffid);
3
  • I'm not sure if this has anything to do with the problem, but why do you have the ; after the } at the end of the second to last line? At best, it's unnecessary. Commented Aug 9, 2013 at 0:02
  • 1
    The answers given to this question are fine but I'm wondering why you have separate pinpass1, pinpass2, etc. functions and whether they maybe contain lots of duplication... would it not be possible to make a single function pinpass(id) that takes staffid as an argument? Commented Aug 9, 2013 at 0:15
  • By the way setting staffid = id is not doing anything in this function; you could just refer to id. Commented Aug 9, 2013 at 0:22

4 Answers 4

1
document.getElementById('bypass').onclick = pinpass2;

That should work just fine. pinpass2 is already a function, you can assign it to onclick like any other object (yes, functions are objects in Javascript). So just change the onclick when you need it.

If you can't detect changes to the result of staffid(), then you should use a switch instead.

document.getElementById('bypass').onclick = function() {
    switch(staffid()) {
        case 1: pinpass(); break;
        case 2: pinpass2(); break;
        default: pinpass3(); break;
    }
};

Though most of the time you don't have to do this. Also, I'm not sure if staffid is supposed to be a function or a variable, but it doesn't change anything.

By the way, this way of attaching handlers is quite old. There's a more powerful one:

document.getElementById('bypass').addEventListener('click', pinpass2, false);

With that you can attach more than one function. To remove one:

document.getElementById('bypass').removeEventListener('click', pinpass2, false);
Sign up to request clarification or add additional context in comments.

Comments

0

You can change the onclick attribute the same way you'd change any attribute ?

var elem = document.getElementById('bypass');

elem.setAttribute('onclick', 'pinpass' + staffid + '(); return false;');

FIDDLE

3 Comments

hmm this seems likely but gives an error something like invalid lefthand side in assignment.
@user1927512 - fixed that, I was to focused on the onclick property.
perfect thank you. to all the others that suggested help thanks too, I think my explanation was poor as i should have mentioned my calls were dynamic and therefore required the values to be changed based on previous interactions.
0

In javascript functions are first class so you can literally just assign pincheck to another variable like this.

var pinpass2 = pincheck;

Now you can still call it like this

pinpass(1,2,3,4);

5 Comments

Thanks, but please explain how i can add the variable to the end. i.e var pinpass + staffid = pincheck;
@user1927512 which part are you confused about
@user1927512: It would be window.pass = window["pinpass"+staffid]; in your case.
@Bergi since you've figured out his issue you mind if I put that in my answer
sorry again im new but your example seems to be adding a variable to the pinpass function call and not changing pinpass() to pinpass1() or pinpass2() etc
0

I'm not 100% from your question, but it looks like you are trying to call a different function based on the staffid variable. I.E. if it is 2 you want to call pinpass2().

If this is a global function you can call window[ 'pinpass' + staffid ]() and it will call the function you want (if it exists).

EXAMPLE: if staffid = 2 then window[ 'pinpass' + staffid ]() is eqivalent to window[ 'pinpass2' ]() which is the same as calling pinpass2().

This works because all global vars (including functions) are properties of the window object, and properties can be accessed using a dynamically generated name and square bracket notation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.