View Full Version : Getting tag contents

07-06-2011, 09:09 PM
I'm working on stripping specific HTML tags in order to do a selected string comparison.
Stripping the tag, done.
Re-calibrating the selection start and end points, done.
Capturing the tags for re-insertion, done.
Getting the start point of the tag, done.

What I am having trouble with is finding the content between the tag so that I can put it back after altering the selected text.

<span class="something">find this content!</span>

Any ideas are more than welcome!

07-07-2011, 01:31 PM

Add an ID to the element.

<span class="something" id="something">find this content!</span>

Then use the following script to get the text from inside.

var x = document.getElementById('something').innerHTML;

var x is now the content between the <span></span> tags so long as the id tag is set as 'something'.

07-07-2011, 01:32 PM
Then use:

document.getElementById('something').innerHTML = x;

to replace the content when ready!

07-07-2011, 04:51 PM
Thanks for the reply.

Although this would work what I'm doing would not always give me control of the "original" content.
I'm working on a CMS and have found severe issues with Tinymce's code clean-up when dealing with a php driven site. eg: external headers.

With what I had captured already I did find a solution.

var re = new RegExp('<'+tag+'[^><]*>|<.'+tag+'[^><]*>','g');
// get tag contents and position for replacement
var tag1 = str.match(re);// array, opening tag, closing tag
var tag2 = str.search(re);// start of opening tag
var tag3 = str.search(tag1[1]); //start of closing tag
var tag4 = tag3 - tag2 - tag1[0].length;// length of content between tags

// tag positions and content stored, safe to remove tags

It might be a bit cumbersome, but it appears to have gotten me to the next point of frustration :)

07-07-2011, 05:12 PM
It looks a bit cumbersome but if it works for you then it works =)