Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery remove all first span elements

Given the following HTML:

<h2><span>1</span><span>2</span></h2>
<h2><span>1</span><span>2</span></h2>
<h2><span>1</span><span>2</span></h2>
<h2><span>1</span><span>2</span></h2>

I would like to remove the first span element from all the h2 elements.

I tried the following which only remove the first span from the first h2:

$("h2 span:first").remove();
$("h2 span").first().remove();
like image 929
capdragon Avatar asked Aug 05 '26 08:08

capdragon


2 Answers

Try using the :first-child selector:

$("h2 span:first-child").remove();
like image 90
Alnitak Avatar answered Aug 06 '26 20:08

Alnitak


Try this

$("h2").each(function(){
   $(this).find("span:first").remove();
});
like image 40
ShankarSangoli Avatar answered Aug 06 '26 22:08

ShankarSangoli