user5871784
user5871784

Reputation:

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();
}

Upvotes: 1

Views: 190

Answers (3)

Ajay Kumar
Ajay Kumar

Reputation: 1342

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();
 }

Upvotes: 0

mike tracker
mike tracker

Reputation: 1071

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>

Hope this helps..:)

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

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.

Upvotes: 4

Related Questions