0

I am trying to move an element inside a form and then change the display to block. I am getting the display to change but can't seem to move it, this is the code I am using

<form action="testsubmit.php">
    <div id="placehere">
    </div>
    <input type="submit" value="submit">
</form>

<div id="input1" style="display:none">
<input type="text" name="test" id="input1">
</div>

<button onclick="test()">Click me</button>


<script>
function test(){
    document.getElementById('input1').appendChild(document.getElementById('placehere'));
    document.getElementById('input1').style.display = 'block';
}
</script>
3
  • Its working fine. jsfiddle.net/6qxurbuw Commented Mar 7, 2016 at 14:52
  • Your code works fine, are you trying to display the field next to the button by any chance? Commented Mar 7, 2016 at 14:56
  • Not quite, it needed to be above the submit button, I had the id's the wrong way round Commented Mar 7, 2016 at 14:56

3 Answers 3

4

the insertion has to be done in the other way round

document.getElementById('placehere').appendChild(
     document.getElementById('input1')
);

The DOM insertion via appendChild() is in fact <parentNode>.appendChild(<node>)

Then, to show the element javascript is not necessary. just hide the input by default via CSS, and show it when it is inside the form element

#input1 { display: none }
#placehere #input1 { display: block }
Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried:

     document.getElementById('placehere').appendChild(document.getElementById('input1'));

Comments

0

Try this code

<form action="testsubmit.php">
    <div id="placehere"></div>
    <input type="submit" value="submit">
</form>

<div style="display:none">
<input type="text" name="test" id="input_text">
</div>

<button onclick="test()">Click me</button>


<script>
function test(){
    document.getElementById('placehere').appendChild(document.getElementById('input_text'));
    document.getElementById('input_div').style.display = 'block';
}
</script>

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.