tags:

views:

44

answers:

2

Hi,

This will be probable quite odd question. But i thought I will give it a go.

Is there a way to remove everything from body apart of one tag that has particular class?

in my case it is an a tag with href.

I need to "fish out" the a tag and then display on DOM just the href tag in lets say p tag.

<body>
 <a class="avatar_user_name" href="/product/xyz">Title</a>

...other body tags and text

<a class="avatar_user_name" href="/product/abc">Title</a>
</body>

and as a result I need to have something like this

<body>
<p>xyz</p>
<p>abc</p>
</body>

Thank you for any help in advance Regards

A: 

I can't think of any way of doing this. I think a better option would be either grab or code the values of the 2 links in JS, remove everything inside the body and then add the links back in. Maybe not the most efficient, but usually people don't remove everything inside a body tag either.

Another option is to add a tag around everything else within the body tag (if you page structure is as simple as your example in the question). This tag doesn't need to be a formatting tag, but could be a span with an ID. In this case you'd just remove everything inside this tag.

Darryl Hein
+3  A: 

Not sure if this will work, but worth a try?

<script type="text/javascript>
$(document).ready(function() {

    // obtain list of links
    var a = [];
    $('a.avatar_user_name').each(function(i,e) {
        a[i] = $(this).attr('href');
    });
    // build up a new page
    $('body').empty();
    for (var i = 0, j = a.length; i < j; i++) {
        $('body').append('<p>' + a[i] + '</p>');
    } 
});
</script>

Let us know if this works, I'd be curious if it does! Good luck!

Funka
Wow, I'm truly amazed. This works perfectly! Thank you so much!
Dom
Nice - didn't know about the empty method. I was thinking about posting an answer using the not selectors and the remove method - this is much better than that would have been.
David Hall