2

What I am trying to figure out is how to make a button, that when you click, will replace itself with a textbox. I found this code on the W3Schools website, and can't figure out how to put javascript (or HTML) elements in.

<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<p id="demo">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var res = str.replace("Microsoft", "W3Schools");
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>
<input type="text" name="textbox" value="textbox"><br>

In the end I want to be able to replace a button with the textbox I put outside the html tags

1
  • "In the end I want to be able to replace a button with the button I put outside the html tags" That's the wrong approach. Either use a string containing HTML or better, create a new DOM element and use DOM manipulation methods to insert that element. See quirksmode.org/dom/intro.html for an introduction. Commented May 28, 2014 at 23:19

2 Answers 2

3

I would not suggest you the innerHTML replacement method.

Here are the steps where you can use replaceChild

  1. Get the parent node of the selected element
  2. use replaceChild

Here the code

// create the new element (input)
var textBox = document.createElement("input");
textBox.type = "text";

// get the button
var button = document.getElementById("demo");
// reference to the parent node
var parent = element.parentNode;
// replace it
parent.replaceChild(textBox, button);

On older browser you probably need a slighlty different solution.

var parent = button.parentNode;
var next = button.nextSibling;
// remove the old
parent.removeChild(button);
// if it was not last element, use insertBefore
if (next) {
   parent.insertBefore(textBox, next);
} else {
   parent.appendChild(textBox);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sounds like what your saying might work but could you put it in jsfiddle or in a comment for me, I'm still a beginner and don't know how to do what your explaining yet.
You should probably show how exactly the input is created (instead of using createTextBox).
@Thomas feel free to try the suggested code and let me know if it worked for you.
@Luke in the first one I'm not sure where to put the "reference to the parent node", and the "replace it". and In the one for older browsers I don't know where any of it goes
what do you mean with "where it goes"? in you function obviously. Its the same as your other code but using node replacing.
-1

I ended up finding a code that works off of http://www.webdeveloper.com/forum/showthread.php?266743-Switch-Div-Content-Using-Javascript

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.