0
<div class="Test">
 name - date   //First name ,Last name - 7/24/2013
 <button type="button"/>

How do i update the part of HTML text inside DIV using Jquery.

I need to update the date section alone inside the div. What i tried is like having the date section in a span with class name and update the text.

But there must be a way to update the text without having <span> .

Am looking for a fix without using

Thanks

3
  • I doubt there is. The only way without wrapping the date inside another element is to update all the text inside div.Test Commented Jul 24, 2013 at 6:21
  • Do you only want to update the date? Leave the name as is? Commented Jul 24, 2013 at 6:21
  • 1
    You can use a regex expression but that would be an overkill. Using an extra element wrapping it and targeting is the simplest among all Commented Jul 24, 2013 at 6:21

6 Answers 6

2

I will suggest you a bit of different way, using regular expression. (Although probably the best way is to use an extra span element.)

var testPattern = /date/;
var divToTest = $('.Test').html();

divToTest = divToTest.replace(divToTest.match(testPattern),'yourDate');
$('.Test').html(divToTest);

Also keep in mind, that if the date is for example dd.mm.yyyy format , you just need to change your reg exp for the specific format. Hope that helped :). (EDITED: Thanks to @Geoff Appleford)

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

2 Comments

This works but you need to put the changed html back into the div: $('.Test').html(divToTest);
@Geoff Appleford Thanks for the catch.(EDITED) :)
1

Well, there is a way but i really suggest you to use extra span element.

Create function that fills your div with text

function fillDiv(date) {
    var html = 'Your text here and after that put ' + date;
    $('#myDiv').html(html);
}

And call this function on page load and on every next change.

2 Comments

This causes the button to be lost
Sure, but its not a big deal to avoid that :)
1

Try

var node = document.createTextNode("asdf");
$($('.Test').contents().get(0)).replaceWith(node)

Demo: Fiddle

Comments

0

If your content has exactly structure like name - date, so you can split your content by - and then replace the second item in your array. Finally you can merge items into a string.

Comments

0

Expanding on what Phong said, the code to do so would look something like:

var children = $('.Test').children();
$('.Test').html($('.Test').html().split('-')[0] + '- ' + newDate).append(children);

That said, this should only be used if for some reason you can't wrap the date in a span.

2 Comments

But then you're losing a button
Oops, you're right, I didn't see the button. My last edit should fix that.
0

Try this

var dateTxt = $('.Test').text().split('-');
$('.Test').text(dateTxt[0] + ' - date');

Comments

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.