Add HTML based on Toggle Jquery

删除回忆录丶 提交于 2019-12-11 17:31:03

问题


I have a toggle that was just made for a class I am getting to work. I need to add in hidden HTML based upon the toggle state.. Basically it needs to submit with the form with the state of the button.. Would this be the best way to grab it? I am posting the form also.

Here is what I have.. When I click the button, it adds the example text, but I need it to go away when I click again..

 $(document).ready(function () { 

    $(".visibilitybutton").click(function(){
        $(this)
            .toggleClass("hide")
            .find("span").toggleClass("icon84 icon85")
            $('.buttons_secondary').append("<input type='hidden'>");
    });

 });

回答1:


 $(document).ready(function () { 

     $('.visibilitybutton').toggle(function() {
          var $button = $(this);

          $button.prop("title","Invisible");
          $button.find('span').removeClass('icon84').addClass('icon85');
          $('.buttons_secondary').append('<input id="visibility_setting" class="hidden" type="hidden" />');
    }, function() {
          var $button = $(this);

          $button.prop("title","Visible");
          $button.find('span').removeClass('icon85').addClass('icon84');

          // OR Remove by id
          $('.buttons_secondary').find('#visibility_setting').remove();
    });

 });



回答2:


You can remove the html you append by id or class like so:

$('.buttons_secondary').append('<input id="hdf_Test" class="hidden" type="hidden" />');

// Remove by class
$('.buttons_secondary').find('.hidden').remove();

// OR Remove by id
$('.buttons_secondary').find('#hdf_Test').remove();

Based off of your previous question, I think you should try this:

$(document).ready(function () { 
    $('.button').toggle(function() {
          var $button = $(this);

          $button.prop("title","Invisible");
          $button.find('.icon85').toggleClass('icon85 icon84');
          $('.buttons_secondary').append('<input id="hdf_Test" class="hidden" type="hidden" />');
    }, function() {
          var $button = $(this);

          $button.prop("title","Visible");
          $button.find('.icon85').toggleClass('icon84 icon85');

          // Remove by class
          $('.buttons_secondary').find('.hidden').remove();

          // OR Remove by id
          $('.buttons_secondary').find('#hdf_Test').remove();
    });
 });


来源:https://stackoverflow.com/questions/11618240/add-html-based-on-toggle-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!