javascript - How to check if element has HTML in it before removing? jQuery -


in function need check if (div, span, p) contains .html elements in before attempting remove html , add in new content.

not sure how this...

i tried this, not working:

// here below tried check see if div's have html, did not work     if ($('.'+rowname+' div').html) {         $('.'+rowname+' div').html.remove();         $('.'+rowname+' span').html.remove();         $('.'+rowname+' p').html.remove();     } 

full function

// create role / fan rows function rowmaker (rowname, rolename) {     //alert(rowname+' '+rolename);      // here below tried check see if div's have html, did not work     if ($('.'+rowname+' div').html) {         $('.'+rowname+' div').html.remove();         $('.'+rowname+' span').html.remove();         $('.'+rowname+' p').html.remove();     }      // blue button     $('.'+rowname+' div').append(rolename);     $('.'+rowname+' div').attr('id', 'blue-fan-'+rolename);      var bluebutton = ('blue-fan-'+rolename);     console.log('bluebutton = '+bluebutton);      // genres     $('.'+rowname+' span').append(roletype);      // tags     $.each(role_actor, function(index, item) {         $('.'+rowname+' p').append(item+', ');     });      $('#'+bluebutton).click(function () {          console.log('clicked blue button');          // clears role_actor used recapture checkboxes         role_actor = [];          console.log('role_actor = '+role_actor);          //$('#modal-'+roleid).modal();         $('#modal-'+roleid).modal({persist:true});         return false;     });  } 

html method not property, need use (), can use length property of string object checking length of returned html string:

if ( $.trim( $('.'+rowname+' div').html() ).length ) {     // $('.'+rowname).find('div, p, span').remove();     $('.'+rowname).find('div, p, span').empty(); } 

note if want change html content of element, there no need remove current html content of it. html method overrides current html content.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -