Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove next div with JavaScript, without jquery

anyone can help me on a things , I have in a page once <nav class='test'> and below a <div> without any class/id, I want to hide/remove <div> element which is in next of <nav> tag , I can do $('.test').next().remove(); but strictly jQuery is not allowed , please give me any trick of JavaScript or CSS, Please note that , i haven’t access to edit any html code only i can add js/css.

Code is looking like this:-

<body class='home'>
    <nav class='test'></nav>
    <div>Want to remove this elements</div>
</body>

Thanks in advance.

like image 827
Amit mishra Avatar asked Aug 05 '26 19:08

Amit mishra


1 Answers

Use querySelector with css adjacent sibling or next-sibling selector.

var ele = document.querySelector('.test + *')

ele.remove();

// or for older browser
// ele.parentNode.removeChild(ele)
// or
// document.body.removeChild(ele)
<body class='home'>
  <nav class='test'></nav>
  <div>Want to remove this elements</div>
</body>
like image 57
Pranav C Balan Avatar answered Aug 08 '26 10:08

Pranav C Balan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!