Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript to alter content of a <div>

How do you alter the content of a <div> using javascript when the <div> has no id or class.

I know that using something like document.getElementById('foo').innerHTML = 'Your content'; Could have been used if the <div> had an id. But in my case the content I want to alter is like this

<div unselectable="on" style="position: absolute; padding: 2px; top: 0px; left: 1px; background-color: rgb(255, 102, 0); color: white;">BOO</div>

I am partial to using document.getElementsByTagName("div")[0].innerHTML = "BOO"; to find it but how do I then proceed to modify its content? Also is this a good way to doing things?

like image 511
Rick Roy Avatar asked Mar 16 '23 01:03

Rick Roy


1 Answers

No there are other div too but that is the only one with that content Boo

If that case, you can find by :contains

$("div:contains('BOO')").html('hello');
like image 191
Norlihazmey Ghazali Avatar answered Mar 23 '23 00:03

Norlihazmey Ghazali