jquery - Dynamically adding and removing element from HTML DOM -
i working on html page there list of items in table , on clicking item new tab generated showing details of item clicked. using jquery-ui tabs dynamically creating tabs. need open tab item if tab not exist item. use below code achieve this
if(! $('#selector').length ) { //create new tab item }
i use tabs unique id each item. code works when loading tab first time, problem have x button close tab , on click f button remove tab division html dom using jquery .remove() function. way tab gets removed when try add tab particular item after removing once. above code doesn't work shows tab exist. , new tab not getting created. how can remove element html dom above code works recreating tabs after removing once. in advance
<body> <div id='tabs'> <ul> </ul> </div> </body> <script> $(document).ready(function(){ $("#tabs").tabs(); $(document).on('click', '.clickable_sellername', function(){ debugger; var sellerid = parseint(this.id.split('_')[2]); var sellername = $(this).text(); if($('#tabindex_'+sellerid).length == 0){ $.ajax({ type:"get", url :"/myadmin/loadprofile", data:{ 'sellerid':sellerid }, datatype:"html", success: function(data){ // add new tab $("#tabs ul").append("<li id='tabindex_"+sellerid+"' ><a href='#sellertab_" + sellerid + "'>" + sellername + "</a><a href='#' id='close_"+sellerid + "' class='ui-tabs-anchor remove' role='presentation' >x</a></li>"); // add content newly added tab $("#tabs").append("<div id='sellertab_"+sellerid+"' ></div>"); $('#sellertab_'+sellerid).html(data) // refresh tabs add newley added tab $("#tabs").tabs("refresh"); } }); return false; } }); $(document).on('click', '.remove', function(){ // tab number of pressed close button var sellerid = parseint(this.id.split("_")[1]); // remove tabindex $('#tabindex_'+sellerid).remove(); // remove tab content $('#sellertab_'+sellerid).remove(); //refresh tabs $('#tabs').tabs("refresh"); }); });
$('#selector').length
returns non 0 value "list" element after removing dom using jquery .remove() whereas in case of "div" element returns 0 above expression after removing div using .remove(). used "div" element checking purpose ie use
if($('#sellertab_'+sellerid).length == 0)
instead of
if($('#tabindex_'+sellerid).length == 0)
Comments
Post a Comment