lilianda Posted February 3, 2014 Share Posted February 3, 2014 hello, 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... Thanks Link to comment Share on other sites More sharing options...
Don E Posted February 3, 2014 Share Posted February 3, 2014 Try removing .event. from this: edit_btn.event.onclick=view_lead_save_field(json_param); edit_btn.onclick=view_lead_save_field(json_param); Link to comment Share on other sites More sharing options...
lilianda Posted February 3, 2014 Author Share Posted February 3, 2014 Amazing ! Thanks you so much for this fast and efficient answer ! Link to comment Share on other sites More sharing options...
Hadien Posted February 3, 2014 Share Posted February 3, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now