Azeem Haider Azeem Haider - 2 months ago 20
HTML Question

Remove next element in jquery

I want to remove next element using jquery here is my code please check it.

<span id="t1">Test 1</span><br/>
<span id="t2">Test 2</span><br/> // I want to remove this <br/>
<span id="t3">Test 3</span><br/>


Here is a jquery code but this is not working.

$('#t2').next().remove();


I want to remove
<br/>
which is after the
t2
.

Answer

Try this :

$(document).ready(function(){

    $("button").on("click",function(){

        $("#t2").next().remove("br");

    })
})

Final code :

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    
    
    <span id="t1">Test 1</span><br/>
    <span id="t2">Test 2</span> <!--I want to remove this--><br>
    <span id="t3">Test 3</span>
    <br><br>
    <button>Remove</button>
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    
    <script>
        
    $(document).ready(function(){

        $("button").on("click",function(){

            $("#t2").next().remove("br");

        })
    })
    </script>
     
    </body>
</html>

Comments