rich text editor...add class on the fly

538 Views Asked by At

I'm using the next code block to insert an image on a editable div. I want to know if it's possible to add a class on the fly to the image...

function insertImg() {  
var src = prompt('Please specify the link of the image.');
if(src)
{
document.execCommand('insertImage', false, src); 
}}

something like this:

<img src="xxxxx.gif" class="myClass">
1

There are 1 best solutions below

1
On

Try this:

var style = document.createElement('style');

style.type = 'text/css';

style.innerHTML = '.myClass { }';

document.getElementsByTagName('head')[0].appendChild(style);

document.getElementsByTagNames('img').className = 'myClass'; // or use getByID.....