-1

I'm trying to have an anchor tag removed and replaced with a span tag but keep the class attr. I have this code but it is showing "undefined" for the class.

$("#wprmenu_menu").find('li:has(ul) > a').replaceWith(function() {
    var Qthis = $(this);
    var className = Qthis.attr('class');
    return $("<span class=\"" + className + "\">" + Qthis.html() + "</span>");
});

the anchor tag gets removed, and the text inside stays but the class does not transfer over.

vnetkc
  • 116
  • 9

2 Answers2

0

Don't see anything wrong with your code. I made a simply assumption based on your selector. Changed the escape to single quotes as well. Not affecting the outcome. Fiddle

$("#wprmenu_menu").find('li:has(ul) > a').replaceWith(function() {
    var Qthis = $(this);
    var className = Qthis.attr('class');
    return $("<span class='" + className + "'>" + Qthis.html() + "</span>");
});
tmcc
  • 1,010
  • 8
  • 12
  • Thanks @tmcc for your response, sorry for my belated response. This snippet did not end up being the issue, and I'm not sure at this point what the real issue was. I do agree that it needed an easier to read quote syntax. I have come a long way since then in JS, lol. – vnetkc Oct 06 '16 at 16:37
0
$('#myButton').click(function(){
    $("#someDiv a").replaceWith(function(){
        return $("<span>" + $(this).html() + "</span>");
    });
});
goto
  • 7,908
  • 10
  • 48
  • 58
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Feb 28 '17 at 13:44