1
    <td class="row" id="253">
    <select class="available-room" style="" id="roomToBook">
<option value="0">Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>`enter code here`
    </td>
     <td class="row" id="253">
    <select class="available-room" style="" id="roomToBook">
<option value="0">Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>              </td>
     <td class="row" id="253">
    <select class="available-room" style="" id="roomToBook">
<option value="0">Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>              </td>

I have a three table cell having different id. Here i want to get the id when user select the option of respective id and add id in a array when user select the two or more then two option value from different table cell in a same time. I have a javascript code also.It shows same id in alert frequently in every selection of option value of same id.Please someone help me.

 $(document).ready(function() { 
 $(document).on('change', '.available-room', function () {
     var room_id =  new Array();
      room_id.push($(this).parent('td').attr('id'));
      alert(room_id);
    });
     });    
2
  • Your HTML is invalid. id attributes must be unique in the document, you can't use the same id on more than one element. So that's the first thing to fix. If you need to group elements together, class is useful. (You'll also get people telling you ids can't be numeric. They can, but if you need to use CSS selectors with numeric id values, it's a pain. [The only restrictions are they must be unique, can't contain spaces, and must have at least one character if given.]) Commented Mar 7, 2017 at 6:32
  • You are right sir, i forgot to change the id value during asking the question. This id attribute is all time unique in my project , which comes from the database.Thanks for your complement. Commented Mar 7, 2017 at 7:34

1 Answer 1

1

You must declare id as unique in a file so you can get value by $(this).val() and array declaration var room_id = new Array() must be outside .on('change',function(){....});

        <table>
            <td class="row" id="roomToBook_1">
                <select class="available-room" style="" id="roomToBook1">
                    <option value="0">Select</option>
                    <option value=1>1</option>
                    <option value=2>2</option>
                    <option value=3>3</option>
                    <option value=4>4</option>
                    <option value=5>5</option>
                    <option value=6>6</option>
                    <option value=7>7</option>
                    <option value=8>8</option>
                    <option value=9>9</option>
                    <option value=10>10</option>
                </select>
            </td>
            <td class="row" id="roomToBook_2">
                <select class="available-room" style="" id="roomToBook2">
                    <option value="0">Select</option>
                    <option value=1>1</option>
                    <option value=2>2</option>
                    <option value=3>3</option>
                    <option value=4>4</option>
                    <option value=5>5</option>
                    <option value=6>6</option>
                    <option value=7>7</option>
                    <option value=8>8</option>
                    <option value=9>9</option>
                    <option value=10>10</option>
                </select>              </td>
            <td class="row" id="roomToBook_3">
                <select class="available-room" style="" id="roomToBook3">
                    <option value="0">Select</option>
                    <option value=1>1</option>
                    <option value=2>2</option>
                    <option value=3>3</option>
                    <option value=4>4</option>
                    <option value=5>5</option>
                    <option value=6>6</option>
                    <option value=7>7</option>
                    <option value=8>8</option>
                    <option value=9>9</option>
                    <option value=10>10</option>
                </select>              </td>
        </table>


        <script>

            $(document).ready(function () {
                var room_id = new Array();
                $(document).on('change', '.available-room', function () {

                    room_id.push($(this).val());
                    //alert(room_id);
                    alert(JSON.stringify(room_id));
                });
            });


        </script>     
Sign up to request clarification or add additional context in comments.

3 Comments

Not working sir. I want the id attributes not the selected option value. And another thing is that the id should be added in the array not in every selection but in one selection of option value.
Then you use $(this).attr("id"); to get id
I have done this already. The main problem is that alert frequently throw the same id when selecting the different option value. Do you have any idea about to get only one id ? Please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.