0

I am having the following drop down input tag and I want to get the default vased on the data-id attribute. I have tried the solution below but does not work.

HTML

 <select id="expiryMonth" data-id="ExpiryMonth">
                        <option value="01" selected="selected">January</option>
                        <option value="02">February </option>
                        <option value="03">March</option>
                        <option value="04">April</option>
                        <option value="05">May</option>
                        <option value="06">June</option>
                        <option value="07">July</option>
                        <option value="08">August</option>
                        <option value="09">September</option>
                        <option value="10">October</option>
                        <option value="11">November</option>
                        <option value="12">December</option>
                    </select>

Jquery

$(function(){
  var expiryYear = $('input[data-id="ExpiryMonth"]');
});
0

2 Answers 2

1

by data-id:

-text

$('select[data-id="ExpiryMonth"] option:selected').text();

-value

$('select[data-id="ExpiryMonth"] option:selected').val();



by id

-text

$("#expiryMonth option:selected").text();

-value

$("#expiryMonth option:selected").val();
Sign up to request clarification or add additional context in comments.

Comments

0

The below code worked for me

   var expiryMonthText = $('select[data-id="ExpiryMonth"]').find("option:selected").val();


   var expiryMonthVal = $('select[data-id="ExpiryMonth"]').find("option:selected").text();

Comments

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.