Anees v
Anees v

Reputation: 323

How to replace some text inside body without affecting existing tag using javascript

I will explain with an example what I need to get done

<body>
    some text 
    <a href="http://somelink.com" class="some">some</a>
    <span class="test">test</span>
</body>

I need to highlight and add link to "some" and "test" in above html by replacing it with JavaScript , how can I do with out affecting other tags and "some" and "test" present in href and class

Note : I don't have access to to edit html of that page all i can do is put my script a the header

Upvotes: 1

Views: 211

Answers (3)

mrrodd
mrrodd

Reputation: 126

You can add a span around "some" and "text" with a specific class and then use jQuery class selector to manipulate as needed.

<span class="someSpan">some</span> <span class="textSpan">text</span> 

$( ".someSpan" ).css( "border", "1px solid red" );

Upvotes: 1

Anto Subash
Anto Subash

Reputation: 3215

you can select them using the class and make your changes.

<body>
    some text 
    <a href="http://somelink.com" class="some">some</a>
    <span class="test">test</span>
</body>

$('span.test').text('Some new text');
$('a.some').text('Some new link text').attr('href',"http//newlink.com");

Upvotes: 0

Alen
Alen

Reputation: 1788

<body>
    some text 
    <a id="link" href="http://somelink.com" class="some">some</a>
    <span id="span" class="test">test</span>
</body>

$("#link").text("some other text");

$("#span").text("some other text");

Upvotes: 0

Related Questions