tibewww
tibewww

Reputation: 603

remove only first <br> of a div by jquery

I'm trying by jQuery to delete only the first <br> of a specific div.

I'm using the following jQuery:

if($(window).width() < 750 ){
    $('[data-url-id=section4]:lt(2) br').remove();
}

Which delete all my br instaed of the first one only from the div

If i replace the lt(2) by lt(1) or other, it does't work.

<h2 style="text-align:center;" id="yui_3_17_2_1_1452876314695_505"> And we’re only just beginning to see<br>
the tip of what that looks like: <br><br>
 It’s the sharing economy, internet of things,  the <br>
 quantified self,  the possibilities of virtual reality&nbsp;and robotics. It’s even shaping ideas about global
  leadership.And underneath all of this, there’s a more fundamental shift taking place, a rise
   in a new way of doing things.</h2>

Upvotes: 0

Views: 333

Answers (3)

Jeroen Noten
Jeroen Noten

Reputation: 3624

That is because you are now searching for the first [data-url-id=section4] element and then all brs inside that one. Instead you want to search for the first br element:

$('[data-url-id=section4] br:lt(1)').remove();

BTW, lt(1) means 'lower than 1', so only the first one (note that it's 0-based).

Upvotes: 0

devlin carnate
devlin carnate

Reputation: 8602

Use the :first selector, like so:

$('[data-url-id=section4] br:first').remove();

JSFiddle Demo

Ref: :first selector

Upvotes: 0

Ben
Ben

Reputation: 2441

This is what you're looking for

https://api.jquery.com/first/

$('[data-url-id=section4] br').first().remove();

Upvotes: 1

Related Questions