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

  • Home
  • SEARCH
  • 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 277855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:08:43+00:00 2026-05-12T01:08:43+00:00

How can I get the text value from an elements’s child? Say I have

  • 0

How can I get the text value from an elements’s child?

Say I have this code on a page:

<div class='geshitop'>
  &#91; CODE &#93; &#91; <a href="#" onclick="javascript:copy(); return false;">PLAIN-TEXT</a> &#93;
</div>
<div class='geshimain'>
  <pre><div class="text" style="font-family:monospace;">Code goes here...</div></pre>
</div>

The function copy():

<script type="text/javascript">
function copy() {
    var text = this.parent.getElementsByName("text");
    var code = text[0].value;
    var popup = window.open("", "window", "resizeable,width=400,height=300");
    popup.document.write("<textarea name='code' cols='40' rows='15'></textarea>");
    popup.code.value = code;
}

How would I go about getting that child’s data: the <div class "text">. How can I get that from the parent?


I’m still having problems. If there is two codeboxes on one page, then it does not work. Remember, I am unable to use ID’s. It must be classes.

If I was able to use jQuery this would be easy.

  • 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-12T01:08:44+00:00Added an answer on May 12, 2026 at 1:08 am

    Try this:

    Change your HTML slightly. The “javascript:” prefix isn’t necessary inside an onclick handler. Also, pass a reference of “this” to your copy function:

    <div class='geshitop'>
      &#91; CODE &#93; &#91; <a href="#" onclick="copy(this); return false;">PLAIN-TEXT</a> &#93;
    </div>
    <div class='geshimain'>
      <pre><div class="text" style="font-family:monospace;">Code goes here...</div></pre>
    </div>
    

    Having done that, alter your copy function to accept the new parameter. Then you just have to locate the correct node. From the question, I think you are looking for the next <div class=”text”> that is a child of the <div class=”geshimain”> that is the next sibling of the parent node that contains the link that was clicked. This function should accomplish that:

    function copy(node) {
        node = node.parentNode; // <div class="geshitop">
    
        // Loop over adjacent siblings, looking for the next geshimain.
        while (node.nextSibling) {
            node = node.nextSibling;
            if (node.nodeName === 'DIV' && node.className === 'geshimain') {
                break;
            }
        }
    
        if (!node) {
            throw new Error("Could not locate geshimain");
        }
    
        // Locate the <div class="text">
        node = (function () {
            var divs = node.getElementsByTagName('div');
            for (var x = 0; x < divs.length; x++) {
                if (divs[x].className === 'text') {
                    return divs[x];
                }
            }
            return null;
        }());
    
        if (!node) {
            throw new Error("Could not locate text");
        }
    
        node =
        '<textarea name="code" cols="40" rows="15">' + node.innerHTML + "</textarea>";
        popup = window.open("", "window", "resizeable,width=400,height=300");
        popup.document.write(node);
        popup.document.close();
    }
    

    Good luck!

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

Sidebar

Related Questions

I have a database which is in Access (you can get it link text
If I have a text field with SWT, how can I get the field
Logging can get complicated, quickly. Considering that you have some code, how do you
How can I get an application to write debug text to the Event Log
When I print "\[\e[34m\]sometext" I get some text in blue, but can I specify
I can get easily see what projects and dlls a single project references from
I can get the executable location from the process, how do I get the
I can get the element like this $(#txtEmail) but I'm not sure how to
I have to select an <a> element from the given class of objects. when
For some reason, I can't get this to work. My options list is populated

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.