Vlad Holubiev
Vlad Holubiev

Reputation: 5154

How to remove text between two tags?

I have such a code:

<input type="text">some dummy text i need to remove! <select>F.i.</select>

I tried to to use jQuery for this. But it doesn't seem to work.

$('div').children('select').prev().remove();

It deletes input instead of that text.

So, how to remove text which is simply between tags?

Upvotes: 0

Views: 868

Answers (1)

Blue Skies
Blue Skies

Reputation: 2975

Sometimes it's just simpler to use the native API when working with next nodes.

var select = $("div > select")[0];

select.parentNode.removeChild(select.previousSibling);

You can also make your selector a little more specific if need be.

var select = $("div > input + select")[0];

You could use jQuery to do the remove as well if you did this:

var select = $("div > input + select")[0];
$(select.previousSibling).remove();

which means you could do it in one line:

$($("div > input + select")[0].previousSibling).remove();

but that gets a little hard to read.

Upvotes: 3

Related Questions