2

My Code

I have this html

​<a href="#">
    <i>[please keep this]</i>
    Some Text To Update
</a>​​​​​​​​​​​​​​​​​​​​​​​

And this jQuery

$('a').click(function(){
    $(this).text('Updated Text');
    return false;        
});​

Problem

When the link is clicked I am getting:

<a href="#">Updated Text</a>

Desired Result

I want to preserve the <i> element and get this:

<a href="#"><i>[please keep this]</i> Updated Text</a>

The HTML cannot be modified


play along on jsFiddle

4 Answers 4

2

The best way to do it is the following:

Adding a span:

<a href="#">
    <i>[please keep this]</i>
    <span class="replace">Some Text To Update</span>
</a>​​​​​​​​​​​​​​​​​​​​​​​


$('a').click(function(){
    $('.replace', this).text('Updated Text');
    return false;
});​

Add your text inside a span and then update your span text.

Demo

Without modify html:

$('a').click(function(){
    $(this).html(function(i, text) {
        return text.substr(0, text.lastIndexOf('>') + 1) + 'Updated Text';
    });
    return false;
});

Demo

or

$('a').click(function(){
    var $keep = $('i', this);
    $(this).empty().append($keep).append('Updated Text');
});

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

@naomik Now I have a third :)
Thanks again, Ricardo. I really like the fn(i, text) solution :)
0

maybe this will help..

http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/

Comments

0
$('a').click(function(){
var saveThis = $(this).children('i');

$(this).text('Updated Text').prepend(saveThis);
 return false;        
  });​

I'm assuming that the text to save will always be in an i tag

EDIT: Used prepend instead, now it works in jsfiddle. No HTML change necessary EDIT: removed .clone per @Ariel

4 Comments

Why would you clone it and then use it in text()? Just grab the text() from it as a string and don't clone the node. And in any case you can't use dom elements that way in string concatenation.
edited for a better fix. Still using clone because I'm assuming we want to preserve the i tag itself with the content and the "Some Text to Update" is not constant. It works in the JSfiddle
You don't need clone. Use the original element directly - you don't need a copy of it.
Not the html of the original, the actual original dom element, so the tag is preserved. i.e. just remove clone() it doesn't nothing useful.
0

Yah, that's not easy to do.

Best option is to wrap the part you want to change in a <SPAN>:

<a href="#">
    <i>[please keep this]</i>
    <span>Some Text To Update</span>
</a>​​​​​​​​​​​​​​​​​​​​​​​

$('a span').....

Second best option is to extract the <i> part and reuse it:

var i = $('i', this);
$(this).empty().append(i, ' Updated Text'));

6 Comments

I was about to suggest using a span, also the second option doesn't preserve the <i> tag
She said she cannot change the HTML
I can't change the original HTML :(
Ariel, I can't change the HTML to use a <span>.
@naomik There are two solutions listed, use the second one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.