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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:09:00+00:00 2026-05-22T21:09:00+00:00

I have an element like this: <td> <a>anchor</a> [ some text ] </td> And

  • 0

I have an element like this:

<td>
  <a>anchor</a>
  [ some text ]
</td>

And i need to set it’s text in jQuery, without removing the anchor.

The element’s contents could vary in order (text before or after), and the actual text is unknown.

Thanks

New Update

This is what i came up using, assumes only a single text node:

    function setTextContents($elem, text) {
        $elem.contents().filter(function() {
            if (this.nodeType == Node.TEXT_NODE) {
                this.nodeValue = text;
            }
        });
    }

    setTextContents( $('td'), "new text");
  • 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-22T21:09:01+00:00Added an answer on May 22, 2026 at 9:09 pm

    Neal’s answer is my suggestion. jQuery doesn’t have a way to select text nodes How do I select text nodes with jQuery?.

    Changing your HTML structure will make for the simplest code. If you can’t do it, you can just use the childNodes property looking for nodes of type 3 (TEXT_NODE)

    Here’s some sample code that assumes the last node is the node you want to edit. This is a better approach than replacing the entire contents of the td because you could lose event handlers when you recreate the HTML

    $('a').click(() => console.log('<a> was clicked'))
    
    $('#btn').click(() =>
      $('.someClass').get(0).lastChild.nodeValue = " New Value");
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class='someClass'>
      <a href="javascript:void(0)">anchor</a> [ some text ]
    </div>
    
    <button id='btn'> Change text without losing a tag's handler</button>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an anchor element that looks like this <a href=url><img src=imgurl/>Text</a> I would
I have an anchor element like this: <a href=/link-to-image/ rel=attachment wp-att-7076><img src=/uploads/img.jpg alt= title=
I have, for example, markup like this <div id=content> <p>Here is some wonderful text,
I have an element like this: <div id=foo_<?php echo $id;?> class=hidden></div> I'm trying if
I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
I have a Zend_Form with file element like this: ->addElement('file', 'image', array( 'required' =>
I have an element, like this: <div id=navleft> </div> I want to make this
I have a list of lists that looks like this: x[[state]][[year]] . Each element
I have some HTML like this: <a class=button> <span> <span> <span class=buttonspan>Content</span> </span> </span>
Say I have an element like this... <math xmlns=http://www.w3.org/1998/Math/MathML> <mo class=symbol>α</mo> </math> Is there

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.