1

I've done some research regarding on how to replace/change the original html button to another. But neither the ways are working.

Here are my codes:

Javascript:

var editBtn = "editBtn" + id; //in order to get the id of the edit button
var cSave = document.getElementById(editBtn);
cSave.innerHTML = "<td><button id='saveBtn' onclick='saveChange(" + id + ");' >Save</button></td>";

The code above is just adding a button inside the existing button.

HTML:

<table>
<td>
<%
  out.println("<button id='editBtn" + mc.getId() + "' onclick='editMC(this.id);'>Edit</button>");
%>
</td>
</table>

The mc.getId() is a method to get the "id".

The latest approach that I've tried is mentioned below, but it's not working:

document.getElementById(editBtn).onclick = function() {saveChange(id)};

And:

$(document).ready(function () {
      $("#editBtn").replaceWith("<td><button id='saveBtn' onclick='saveChange(" + mcId + ");' >Save</button></td>");
});

Anyhow I can do to change the edit button to save button while onclick() is performed?

5
  • You tagged this question as jquery, so have you tried to use it? Commented Jun 23, 2014 at 3:25
  • document.getElementById(editBtn) , IS editBtn Variable ? , if not then make it document.getElementById("editBtn") Commented Jun 23, 2014 at 3:29
  • #editBtn is not the ID of the button, it's calling a function that adds something to the ID. Commented Jun 23, 2014 at 3:30
  • @user3743562 In your example you're replacing even the TD element which is wrong. Take a look at my demo. Commented Jun 23, 2014 at 3:40
  • thanks guys, so far the way that Anjum provided works for me :) Commented Jun 23, 2014 at 3:58

2 Answers 2

1

You can change the text of the button from Edit to Save using innerHtml. You are actually putting some html inside the button element. Try

var cSave = document.getElementById(editBtn);
cSave.innerHTML = "Save";
cSave.onclick = function() {saveChange(id)};
Sign up to request clarification or add additional context in comments.

3 Comments

Anjum, the text has changed but the function () {saveChange(id)} does not work upon clicking it again.
cSave.setAttribute("onclick","javascript:saveChange(id);");
Oh my bad, was posting to a wrong .jsp, anyhow thanks, it work :)
1

Wrap the ID in quotes (unless it's a variable name):

document.getElementById("editBtn");

in jQuery it would be between this lines:

var mcId = 'bla';

function saveChange(){
  alert( mcId );
}


$(function () { // DOM ready shorthand


   $("#editBtn").click(function(){
      $(this).replaceWith("<button id='saveBtn'>Save</button>");
   });

  // Than, for your dynamically generated element use:

  $('#parent').on('click', "#saveBtn", function(){
     saveChange( mcId );
  });

});

jsBin demo

note that I used for static element reference a non-dynamic #parent. You should do the same (to prevent bonding to $(document) as it's quite expensive to listen to events all the way up the DOM).

jQuery .on() with event delegation for dynamically generated elements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.