DCJones
DCJones

Reputation: 3461

How to get the ID value of a selected option in a jQuery accordion dropdown menu?

I have a jQuery accordion menu which when clicked opens and allows the user to select an option listed.

<div class="panel panel-default">
  <div class="vertical-menu" id="headingRooms"><a data-toggle="collapse" data-parent="#accordion" href="#collapseFour"><span class="glyphicon glyphicon-plus"> </span> Rooms</a></div>
    <div id="collapseFour" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
    <div class="panel-body">
      <div class="RoomListS">
        <div class="boxscrollRoomListS">
        <?php do { ?>
          <div class="room-list-background">
            <div  data-target="#add_data_modal" name="roomid[]" class="RoomID" data-toggle="modal" data-backdrop="static" data-keyboard="false"  id="RoomID=<?php print $row_AvailSyn5['ConfRoomID']; ?>" target="_self"> <?php print $row_AvailSyn5['ConfRoomName']; ?></div>
          </div>
        <?php } while ($row_AvailSyn5 = mysql_fetch_assoc($AvailSyn5)); ?>
      </div>
    </div>
    </div>
  </div>
</div>

When I inspect the option list, each option has the following with a different number id:

DIV#RoomId=<the id number>.RoomID

When the user clicks on an option, I need to get the id value of the selected option. I am using the following code to try achieve this.

$('.RoomID').click(function(){
  var roomid = document.getElementById('RoomID');
  console.log("ROOM ID ", roomid);
});

The console returns:

"Room ID null"

How can I get this to work?

Upvotes: 1

Views: 289

Answers (2)

Deepak A
Deepak A

Reputation: 1636

use this.id hope it helps

$('.RoomID').click(function(){
  var roomid = this.id;
  console.log("ROOM ID = "+ roomid);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="RoomID" id="RoomId">click here to get id</div>

Upvotes: 0

masoudmanson
masoudmanson

Reputation: 748

This can help you

$('.RoomID').click(function(){
  var roomid = $(this).attr('id');
  console.log("ROOM ID ", roomid);
});

Upvotes: 1

Related Questions