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 6095501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:49:09+00:00 2026-05-23T12:49:09+00:00

I have encountered a very strange bug in Firefox. I have a javascript function

  • 0

I have encountered a very strange bug in Firefox.

I have a javascript function in an external file that works perfectly on regular complexity websites. However I have been putting together a few demonstration examples and come across something odd.

With html formatted like this (in an editor):

<div><p>Q: Where's the rabbit?</p><p class="faq_answer">A: I don't know, honest</p></div>

The Javascript works as expected.

However when like this:

<div>
<p>Q: Where's the rabbit?</p>
<p class="faq_answer">A: I don't know, honest</p>
</div>

It fails at this line:

elementsList[i].parentNode.firstChild.appendChild(finalRender.cloneNode(true));

Why on Earth would formatting of html cause anything at all?

  • 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-05-23T12:49:10+00:00Added an answer on May 23, 2026 at 12:49 pm

    It is not a bug. The DOM has not only element nodes, but also text nodes [docs] (among others). In this example:

    <div>
    <p>Q: Where's the rabbit?</p>
    

    you have at least two text nodes:

    • One between the <div> and the <p>, containing a line-break.
    • One text node inside the <p> element node, containing the text Where's the rabbit?.

    Thus, if elementsList[i].parentNode refers to the <div> element,

    elementsList[i].parentNode.firstChild
    

    will refer to the first text node.

    If you want to get the first element node, use

    elementsList[i].parentNode.children[0]
    

    Update: You mentioned Firefox 3.0, and indeed, the children property is not supported in this version.

    Afaik the only solution to this is to loop over the children (or traversing them) and test whether it is a text node or not:

    var firstChild = elementsList[i].parentNode.firstChild;
    
    // a somehow shorthand loop
    while(firstChild.nodeType !== 1 && (firstChild = firstChild.nextSibling));
    
    if(firstChild) {
        // exists and found
    }
    

    You might want to put this in an extra function:

    function getFirstElementChild(element) {
        var firstChild = null;
        if(element.children) {
            firstChild = element.children[0] || null;
        }
        else {
          firstChild = element.firstChild;
          while(firstChild.nodeType !== 1 && (firstChild = firstChild.nextSibling));
        }
        return firstChild;
    }
    

    You can (and should) also consider using a library that abstracts from all that, like jQuery.

    It depends on what your code is actually doing, but if you run this method for every node, it would be something like:

    $('.faq_answer').prev().append(finalRender.cloneNode(true));
    

    (assuming the p element always comes before the .faq_answer element)

    This is the whole code, you wouldn’t have to loop over the elements anymore.

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

Sidebar

Related Questions

We have encountered a very strange class not found problem in our web app
I have encountered a very strange issue. Whenever I use the .NET membership provider
I have just encountered very strange problem - my GWT app hosted on Google
I have encountered a rather odd error that I do not understand. I created
I have encountered odd behavior when using document.getElementById tonight. Duplicated in Firefox 3 and
I have encountered a problem that I have not come accross yet when setting
I recently encountered very strange behaviour with the UIImagePickerController. On our main view we
I have a very strange situation. I have a large set of records to
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
So far I have encountered adjacency list, nested sets and nested intervals as models

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.