Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:27:24+00:00 2026-06-13T07:27:24+00:00

I have a trouble while split page by Javascript. I want to move 17

  • 0

I have a trouble while split page by Javascript. I want to move 17 p tags into one div tag.
So I have 34 p tags like this:

<p>1</p> <!-- Each p tag have the value is the number of the p tag -->

And then I have this Javascript code:

  var p = document.getElementsByTagName("p");
  var div = document.createElement("div");
  for(i=0;i<17;i++){
    div.appendChild(p[i]);
  }
  document.getElementsByTagName("body")[0].appendChild(div);

And the result I received is in the div tag, there are 17 p tags but the value of these p tag is odd number, which mean like this:

1

3

5

7

9

11

13

15

17

19

21

23

25

27

29

31

33

This is the demo: http://jsbin.com/eyivic

Meantime, I want the p tags in the div tag will look like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

So what is the problem? How can I fix it?
Thanks you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:27:25+00:00Added an answer on June 13, 2026 at 7:27 am

    When you make an appendChild on the div, it removes the element of your p array. This will make this “only odds” behaviour happens. Let’s say that you have 4 p elements.

    1, 2, 3, 4
    

    If you make an appendChild from p[0] it will move your p element as you expected, but now your list is like this:

    2, 3, 4
    

    You are now pointing to p[1], that you were expecting that was the element 2, but it will be the element 3 instead.

    To fix it you can always point to p[0] and it will behave as you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble figuring out why a simple division like this one always returns
Hello I have been having trouble with this for a while now. I have
I have been having some real trouble with this for a while. I am
I have trouble doing this... You may want to get the same result as
We have trouble implementing a nice session mechanism in JSP. One of our requirement
I have trouble with strange behavior of for..in cycle in JavaScript. I have the
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
I have some trouble trying to split large files (say, around 10GB). The basic
I have trouble getting the absolute path with cakephp while using a shell. Shell
I am having trouble while using the YUI panel as a dialog. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.