0

I'm importing an HTML file and I want to remove all href from all elements on it, so the final text doesn't link anywhere and appears like "normal" text.

I'm already taking just a part div#divvy from this HTML, so the process of removing href must works on it and not on all the response.

The final result should go to div#pepeland.

I have this code:

function pepe(){
    $.ajax({
        url: 'http://www.site.com/text.html',
        dataType: 'html',
        success: function(response){
            $('#pepeland').html(jQuery(response).find('#divvy').html());
        }
    });
};

I think but am not sure that maybe I should insert something like one of these following lines inside the above code, but I don't know how to do it.

$(response).querySelectorAll("href").remove();

or

$(response).attr('href', '');

or

response.replace("href", '');

on below answers, i realize that what i want is to remove all the tag without removing the text. Because the words keeps formatted with underline and all the attributes of the tag. So roasted wrote my the right answer for this, see below!

sergius
  • 165
  • 1
  • 1
  • 11
  • your `response` isn't technically available like that. You have to basically append the HTML that comes from your post, then write a line under that post to find the specific links within that appended html – ntgCleaner May 28 '13 at 17:49
  • His response is available like that. When you wrap the returned data as a jQuery object `$(data)`, you can now use jQuery methods and selectors on it, so `$(response).find('[href]').prop('href', '');` will look for any element within the response that has an href property, then will take that property and change it to be nothing. – Ohgodwhy May 28 '13 at 17:50
  • @Ohgodwhy Thank you, I stand corrected. How about just `.removeAttr()`? – ntgCleaner May 28 '13 at 18:00
  • You should not mention the right answer from within your body text, that's what upvotes and more precisely for your "accepted answers" are for. AFAIK, you do not have the privilege of upvotes but accepted answers should be available for you. Please, use those. All in all, the last paragraph seems unnecessary. A question should be a question, an answer should be an answer, and each should stand on its own. – k0pernikus May 28 '13 at 22:22

3 Answers3

1

I would need to see your html file to see how to select the elements but you would need to loop through all the elements to replace them all. So something like this...

$('YOUR SELECTOR').each(function(){
      $(this).contents().unwrap();
});

This will take the anchor tag away and make it purely text.

similar to the answer of this question.. Remove a HTML tag but keep the innerHtml

Community
  • 1
  • 1
rjg132234
  • 590
  • 3
  • 8
  • 21
  • 1
    it should be `.contents()` not `.contents` – wirey00 May 28 '13 at 17:54
  • $(this) would refer to whatever your selector was to start the each function, without your HTML I can't really tell you what your selector should be... but it could be something like $('#pepeland a').each(function...etc. – rjg132234 May 28 '13 at 18:38
  • perfect! i mixed this with roasted answer (below). Thanks!! :) – sergius May 28 '13 at 19:27
  • @sergius "answer below" is a misguiding concept, as answers can appear in different order due to upvotes. Hence, if you want to reference to another answer, please use its share-link. Also, thanks via comments are not needed, that's what upvotes are for. – k0pernikus May 28 '13 at 22:24
0

Try this:

function pepe(){
    $.ajax({
        url: 'http://www.site.com/text.html',
        dataType: 'html',
        success: function(response){
            var $divvy = $(response).find('#divvy');
            $divvy.find('a').each(function(){$(this).replaceWith($(this).text())});
            $('#pepeland').html($divvy);
        }
    });
};
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

I would do something like this:

$.ajax({
    url: 'http://www.site.com/text.html',
    dataType: 'html',
    success: function(response){
        $('#pepeland').html(response);
        $('#pepeland a').removeAttr('href');
    }
});

This would remove the href attribute on all links inside your appended html

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86