Jump to content

Cannot set property 'onclick' of undefined


Recommended Posts



I'm trying to change my form button when it's clicked.


Here is the HTML/PHP code for my button

 <button id='edit-btn' onclick='view_lead_make_editable_field(<?php echo json_encode($param);?>)'>Modifier</button>

And here is my javascript function:

function view_lead_make_editable_field(json_param){    /*.....some processing....*/var edit_btn = document.getElementById("edit-btn");    edit_btn.innerHTML="Enregistrer";    edit_btn.event.onclick=view_lead_save_field(json_param);}

When I click the button, it's name is well modified to "Enregistrer" but the function associated to the onclick event is not changed. Looking at chrome developper console I can see : Cannot set property 'onclick' of undefined


Why undefined since the button label has changed in the previous line ?


Also, when clicking the button, the function view_lead_save_field(json_param) seems to be called which is even stranger...



Link to comment
Share on other sites

also note that, unless the function "view_lead_save_field" actually returns a function, that edit button won't do anything when you click it.


I assume you'll want it written like this:

edit_btn.onclick=    function(){        view_lead_save_field(json_param);    };

so that it'll run "view_lead_save_field" itself whenever you click the edit button, and not whatever "view_lead_save_field" returns every time you click the edit button. this will also allow the scope (so that it knows what "json_param" is every time you click) to be properly preserved.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...