1

I'm new at programming, so it would be easier for me if you could explain what the code is doing too. But anyway, at a certain point in my Javascript, I want to make a button disappear and replace it with the words that were on the button before. I've tried looking at a few different other questions about things like this, but this one was too confusing, and I didn't understand it, and this one, didn't work for some reason. Can anyone help me?

Thanks

EDIT

Here is some of my HTML:

<div class="inside">
<form name="form" onsubmit="return false">
    <button id="id1" class="buttonthird" name="Button1" onclick="myFunction1()">Button 1</button>
    <button id="id2" class="buttonthird" name="Button2" onclick="myFunction2()">Button 2</button>
    <button id="id3" class="buttonthird" name="Button3" onclick="myFunction3()">Button 3</button><br/>
    <button id="id4" class="buttonthird" name="Button4" onclick="myFunction4()">Button 4</button>
    <button id="id5" class="buttonthird" name="Button5" onclick="myFunction5()">Button 5</button>
    <button id="id6" class="buttonthird" name="Button6" id="p2" onclick="myFunction6()">Button 6</button>
    </form>
    <h1 class="counter"> <span id="number">0</span> guesses</h1>
<div>
2
  • Can you use jQuery, or does this have to be pure js? Commented Jan 30, 2014 at 22:14
  • Please show some code so that we know whether it is an input button or a button tag, has an id, has a class, etc... Commented Jan 30, 2014 at 22:15

2 Answers 2

4

Your HTML:

<div>
    <button id="my-btn">Hello</button>
</div>

Your JS:

var myBtn = document.getElementById("my-btn"),
    mySpan = document.createElement("span");
mySpan.innerHTML = myBtn.innerHTML ;
myBtn.parentNode.replaceChild(mySpan, myBtn);
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't get what you mean
to style using css, after myspan is set add this line to add an id: mySpan.setAttribute('id', 'buttonID'); then style the id accordingly using css
1

Presuming your button is in a containing element e.g.

<div id="button_container"><input type="button" name="x" value="y" /></div>

You can simply replace the contents of the inner div, like so:

document.getElementById('button_container').innerHTML="y";

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.