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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:23:17+00:00 2026-05-23T04:23:17+00:00

$imgs = $xpath->query(‘//img’); $i = 0; foreach($imgs as $img) { $before = new DOMText($captions[$i]);

  • 0
$imgs = $xpath->query('//img');
$i = 0;
foreach($imgs as $img) {
    $before = new DOMText($captions[$i]);
    $img->parentNode->insertBefore($before);
    $i++;
}

I want to insert some texts before a tag however I can’t make this to work. The texts are sometimes inserted into wrong places. How can I solve this?

  • 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-23T04:23:18+00:00Added an answer on May 23, 2026 at 4:23 am

    Try:

    $img->parentNode->insertBefore($before, $img);
    

    Adding $img as an argument (a ‘reference node’) to the insert function explicitly tells insert where in the hierarchy you want to insert the new node. Otherwise the docs simply say “appended to the children”, which means the new node will be the last one of the parent’s children.

    e.g.

       <span>  <-- parentNode
          <b>This is some text before the image</b>
          <img ...>   <--- $img node
          <i>This is some text after the image</b>
       </span>
    

    Without the extra argument, you get:

      <span>
         <b>...</b>
         <img ...>
         <i>...</i>
         new text node here   <-- appended to parent Node's children, e.g. last
      </span>
    

    WITH the argument, you get:

      <span>
         <b>...</b>
         new text node here   <--the "before" position
         <img ...>
         <i>...</i>
      </span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var imgs=document.getElementsByTagName('img'); for(i=0;i<imgs.length;i++){ console.log(imgs[i]); if(typeof imgs[i]!='undefined' && typeof imgs[i].parentNode!='undefined') { (imgs[i].parentNode).removeChild(imgs[i]); } } I
I couldn't find this anywhere, partly because its keywords are pretty common. $('.pause_button').text('<img src=../imgs/icons/control_pause_blue.png
I have the following html: <div id=join-us-subheader-imgs> <div class=subheader-img-wrapper> <img src=/wp-content/themes/lib/join_us/join-us_1.png> <p class=img-quote>This is
i am currently using this code: <div style=width:200px; height:300px;> <img src=./imgs/Apeiron-Island-Hotel.jpg style=width:200px; height:200px;/> <input
have this code: $products = $feed->_xpath->query( //cf:vehicle ); foreach( $products as $product ) {
Here is my code so far. function img_find() { var imgs = document.getElementsByTagName(img); var
This is my HTML: <div id=user-avatar><img src=/imgs/frame.png alt=/></div> user-avatar class is following: #user-avatar {
I have a list full of a imgs: <ul> <li><a href=#><img src=test.png /></a</li> <li><a
here is my code-snippet: <div id=navbar> <a href=index.html > <img class=navbar src=http://ssss.com/imgs/WG_home.png /> </a>Home
i want to create a new variable with a variable name, for example: function

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.