How to delete div with jQuery that is dynamically created with unique id?

How to delete div with jQuery that is dynamically created with unique id? i am trying to use below code but it is not working. HTML CODE

<div class="div-roomcart" id="divOAK1AKL">
  <div class="div-roomcart-hotel">
    <div class="div-roomcart-hotelname">Eden Park Bed And Breakfast</div>
    <div class="div-roomcart-roomtype">Single Standard</div>
  </div>
  <div class="div-roomcart-room">
    <div class="div-roomcart-roomprice">14058.26</div>
    <div class="div-roomcart-roomaction">
        <input type="submit" id="submit" value="Remove" class="btnbook" onclick="removeroom(divOAK1AKL)">
    </div>
</div>

there will be multiple of div which i have to delete on onclick="removeroom(code)" this is a jQuery function below is the jQuery Code jQuery Code in removeroom.js

function removeroom(hotelcode){
   $("'#"+hotelcode"'").remove();
}

Answers


try this one

function removeroom(hotelcode){
   $("#" + hotelcode).remove();
}

or

just pass the perameter with selector like

 removeroom("#hotelcode"); // calling function hare.

 function removeroom(hotelcode){
    $(hotelcode).remove();
 }

There is no need to wrap the selector once again with single quotes,

function removeroom(hotelcode){
   $("#" + hotelcode).remove();
}

That will make the selector invalid. Also you can use an dedicated event handler for it rather than using an inline event handler. Inline event handler has more disadvantages, and the most important one from that is maintenance.


without Jquery,

function removeroom(hotelCode) {
  typeof hotelCode == 'object' ? hotelCode.remove() : document.getElementById(hotelCode.toString()).remove();


}
<div class="div-roomcart" id="divOAK1AKL">
  <div class="div-roomcart-hotel">
    <div class="div-roomcart-hotelname">Eden Park Bed And Breakfast</div>
    <div class="div-roomcart-roomtype">Single Standard</div>
  </div>
  <div class="div-roomcart-room">
    <div class="div-roomcart-roomprice">14058.26</div>
    <div class="div-roomcart-roomaction">
      <input type="submit" id="submit" value="Remove" class="btnbook" onclick="removeroom('divOAK1AKL')">
    </div>
  </div>

Need Your Help

VB open specific Excel Version when two are installed

excel-vba autohotkey vba excel

I am looking at this piece of code that I got from here because I would like to use it. The issue is, I have used 2003 to create the "test.xls" file but when I run this code it opens in 2007. Is ...

Difficulties with Isolated Storage

c# xna

I tried this tutorial: http://wellroundedgeek.com/post/2011/01/25/Simple-XNA-Cross-Platform-Settings-Manager.aspx