Steven Matthews
Steven Matthews

Reputation: 11325

Trying to remove links from variable

$.get("http://en.wikipedia.org/wiki/Afghanistan", function(response) {
  var elements = $.parseHTML(response);
  var wiki = $(elements).find('#mw-content-text').find("p");
  var ps = [];

  var arrayLength = wiki.length;
  for (var i = 0; i < arrayLength; i++) {
    if (wiki[i].innerHTML === "") {
      break;
    }
    var item = wiki[i]
    ps.push(item);
    $("#fakediv").append(ps);
}

I am trying to remove the links from the variable item, but I can't seem to find any examples of how to do this from a variable - everything assumes that I'll be using a selector.

I've tried .removeAttr("href"), but that doesn't seem to work and I'm not quite sure how to remove links and leave the text.

Upvotes: 0

Views: 185

Answers (1)

epascarello
epascarello

Reputation: 207527

You say you want to unlink the links, but you are looping over paragraph elements and trying to remove its attribute. I doubt the paragraph has an href attribute.

So you need to find the anchors inside the paragraph tags

var item = wiki.eq(i);
item.find("a").removeAttr("href")
ps.push(item);

or

var item = wiki.eq(i);
item.find("a").contents().unwrap();
ps.push(item);

Upvotes: 1

Related Questions