Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top