最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove an HTML element from jQuery - Stack Overflow

programmeradmin0浏览0评论

I have the following basic HTML:

<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

When the value SECOND is selected, a jQuery function should remove <h2 id="second">Second</h2>, so I made the following function:

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if (tst == 'second'){
        $('#second').remove()
    }
});

It works without any problem, the trouble is that my current function will remove that element definitively, instead, if the value FIRST is selected after SECOND, my html should go back to the initial state. How can I handle it? Should I use another function instead of remove() here?

I have the following basic HTML:

<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

When the value SECOND is selected, a jQuery function should remove <h2 id="second">Second</h2>, so I made the following function:

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if (tst == 'second'){
        $('#second').remove()
    }
});

It works without any problem, the trouble is that my current function will remove that element definitively, instead, if the value FIRST is selected after SECOND, my html should go back to the initial state. How can I handle it? Should I use another function instead of remove() here?

Share Improve this question edited Feb 4, 2020 at 11:32 ROOT 11.6k5 gold badges34 silver badges48 bronze badges asked Feb 4, 2020 at 11:20 Jack022Jack022 1,2977 gold badges50 silver badges111 bronze badges 1
  • 2 Why not show / hide the elements as per need instead of removing. – Umair Khan Commented Feb 4, 2020 at 11:23
Add a ment  | 

7 Answers 7

Reset to default 4

You can match the current value with id of the element to be shown:

$(document).on('change', '.selector', function() {
    var tst = $(this).val();
    $('#first, #second').hide();
    $(`[id=${tst}]`).show()
});

$('.selector').trigger('change'); // trigger the event on page load to show the element by matching the value
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="first">First</h1>
<h2 id="second">Second</h2>

you should use hide when the selector is second and show other than selector second

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if (tst == 'second'){
        $('#second').hide();
    }
    else{
      $('#second').show();
    }
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option>
<option value="third">Third</option>
</select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

you can show and hide the element instead of removing it. add mon class as heading to both header and show it by default.

see below code

NOTE: correct the spelling of header with id="first"

$(function(){
   $(document).on('change', '.selector', function() {
    $('.heading').show();
    $('#' + $(this).val()).hide()
});
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 class="heading" id="first">First</h1>
<h2 class="heading" id="second">Second</h2>

What you can do is play with the css display: none rule:

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if (tst == 'second'){
        $('#second').css({'display': 'none'})
    } else {
        $('#second').css({'display': 'inline'})
    }
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option>
</select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

It will make the element disappear without removing it pletely from the DOM.

As @Umair mentioned, use show/hide for this.

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    $("#second, #first").show();
    if (tst == 'second'){
      $('#second').hide()
    } else {
      $('#first').hide()
    }
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="first">First</h1>
<h2 id="second">Second</h2>

Jquery show hide will be the better way to do

$(document).on('change', '.selector', function() {
    tst = $(this).val()
    if(tst == 'second')
      $('#second').hide();
    else
      $('#second').show();  
});
<script src="https://ajax.googleapis./ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<select class="selector"><option value="first">FIRST</option><option value="second">SECOND</option></select>

<h1 id="firt">First</h1>
<h2 id="second">Second</h2>

Using change method we can manage hide/show in jQuery.divText set instance for selector according div id will hide & show

$(".selector").change(function() {
  let divText = $(this).val();

  if (divText === 'second') {
    $('#second').hide();
  } else {
    $('#second').show();
  };
}); 
发布评论

评论列表(0)

  1. 暂无评论