Asiri Liyana Arachchi
Asiri Liyana Arachchi

Reputation: 2673

Line breaks after input tags

<br> should be added dynamically. I've used the following script.

HTML code

<input ...../> 
<input ...../> <br>
<input ...../> 
<input ...../> <br>

JS

 var mybr = document.createElement('br');
 var inputs = document.getElementsByTagName("input");

 for (var i = 0; i < inputs.length; i++) {
      if (i % 2 == 0) {
           // inputs[i].outerText=inputs[i].outerText+"<br><br>";
            inputs[i].appendChild(mybr);

      }
 }

But it doesn't work. Any idea?

Upvotes: 2

Views: 816

Answers (2)

Amani Ben Azzouz
Amani Ben Azzouz

Reputation: 2565

youn can replace :

inputs[i].appendChild(mybr);

by:

inputs[i].outerHTML+='<br/>' ;   

    <input ...../> 
    <input ...../> <br>
    <input ...../> 
    <input ...../> <br>


<script type="text/javascript">
     
 var inputs = document.getElementsByTagName("input");
     for (var i = 0; i < inputs.length; i++) {
          if (i % 2 == 0) { 
             inputs[i].outerHTML+='<br/>' ;        
            }
     }
  </script>

Upvotes: 1

Muhammad Usman
Muhammad Usman

Reputation: 1362

Replace this line

inputs[i].appendChild(mybr);

with (by using Jquery)

$(inputs[i]).after(mybr)

but if you want to use Javascript I will refer to see this Answer

Upvotes: 0

Related Questions