Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change only text inside a div which has other divs

Tags:

html

jquery

I have html below. What I want to do depends on some conditions. I want to keep changing the text which is Sometitle.

<div id="header">
    Sometitle
    <div>
        <div>
            aa
        </div>
        <div>
            bb
        </div>
    </div>
</div>

I tried using Jquery below but it removes the other divs also.

$("#header").text("Its a thrusday today !");

How do I get this done? I cannot modify this HTML in any case. I just have to use jquery and get this thing done.

Here is a jsfiddle I created : http://jsfiddle.net/qYUBp/

like image 656
Yasser Shaikh Avatar asked Mar 07 '13 05:03

Yasser Shaikh


People also ask

How do I change the inner text of a div?

const div = document. getElementById('container'); // ✅ Change (replace) the text with HTML div. innerHTML = `<span style="background-color: lime">Replacement HTML</span>`; The innerHTML property gets or sets the HTML contained within the element.

How do I force text to stay inside a div?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

Can I put text directly in a div?

Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.


1 Answers

If you dont want it in a single line.

var tmp=$("#header>div").html();
$("#header").text("its thursday").append('<div>'+tmp+'</div>');

jsfiddle

like image 131
arjuncc Avatar answered Oct 04 '22 08:10

arjuncc