javascript - Jquery add using append and remove using .remove -
i'm trying make 2 buttons acts remove , add function first have this:
html
<video id="localvideo" style="background-color:black"></video> <div id="remotevideos"></div>
buttons
<button id="btnon">on</button> <button id="btnoff">off</button>
script:
$(document).ready(function() { //$("#btnon").click(function() { // $('#a').append("<div id='localvideo'>"); // $('#a').append("<div id='remotevideos'>"); //}); $("#btnoff").click(function() { $("#localvideo").remove(); $("#remotevideos").remove(); }); });
what i'm trying remove 2 div's , have ability return them, condition can add them if missing , remove them if present, therefore limiting them 1 add , 1 remove. how can accomplish this?any suggestion appreciated
you check if #localvideo
element exists. also, if elements in #a
element, can remove them calling $('#a').empty();
.
$(document).ready(function() { $('#btnon').click(function() { if ($('#localvideo').length == 0) { $('#a').append('<video id="localvideo" style="background-color:black"></video><div id="remotevideos"></div>'); } }); $('#btnoff').click(function() { if ($('#localvideo').length > 0) { $("#localvideo").remove(); $("#remotevideos").remove(); } }); });
you consider hiding , showing video elements, rather adding , removing them.
Comments
Post a Comment