Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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,

  • #2
    Regular Coder
    Join Date
    Jun 2011
    Location
    Swindon, England
    Posts
    124
    Thanks
    3
    Thanked 9 Times in 9 Posts

    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'.

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Location
    Swindon, England
    Posts
    124
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Then use:

    Code:
    document.getElementById('something').innerHTML = x;
    to replace the content when ready!

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Location
    Swindon, England
    Posts
    124
    Thanks
    3
    Thanked 9 Times in 9 Posts
    It looks a bit cumbersome but if it works for you then it works =)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •