Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-06-2011, 08:09 PM   PM User | #1
scorpa54
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
scorpa54 is an unknown quantity at this point
Getting tag contents

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!
Thanks,
scorpa54 is offline   Reply With Quote
Old 07-07-2011, 12:31 PM   PM User | #2
LSCare
Regular Coder

 
Join Date: Jun 2011
Location: Swindon, England
Posts: 124
Thanks: 3
Thanked 9 Times in 9 Posts
LSCare is an unknown quantity at this point
Smile

Hi,

Add an ID to the element.

Code:
<span class="something" id="something">find this content!</span>
Then use the following script to get the text from inside.

Code:
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'.
LSCare is offline   Reply With Quote
Old 07-07-2011, 12:32 PM   PM User | #3
LSCare
Regular Coder

 
Join Date: Jun 2011
Location: Swindon, England
Posts: 124
Thanks: 3
Thanked 9 Times in 9 Posts
LSCare is an unknown quantity at this point
Then use:

Code:
document.getElementById('something').innerHTML = x;
to replace the content when ready!
LSCare is offline   Reply With Quote
Old 07-07-2011, 03:51 PM   PM User | #4
scorpa54
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
scorpa54 is an unknown quantity at this point
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.

Code:
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
scorpa54 is offline   Reply With Quote
Old 07-07-2011, 04:12 PM   PM User | #5
LSCare
Regular Coder

 
Join Date: Jun 2011
Location: Swindon, England
Posts: 124
Thanks: 3
Thanked 9 Times in 9 Posts
LSCare is an unknown quantity at this point
It looks a bit cumbersome but if it works for you then it works =)
LSCare is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:05 AM.


Advertisement
Log in to turn off these ads.