0

I have some dropdown list like this :

<select name="qty" class="mb10" onchange="test()" data-plan-type="offer" data-plan-id="7395" data-plan-day="2210009">
    <option value="0">0 rooms</option>
    <option value="1">1 rooms</option>
    <option value="2">2 rooms</option>
    <option value="3">3 rooms</option>
    <option value="4">4 rooms</option>
</select>

And this is my function which called with onchange event :

function test()
{
    var this_plan_type = $(this).data('plan-type');
    var this_plan_id = $(this).data('plan-id');
    var this_plan_day = $(this).data('plan-day');

    alert(this_plan_type);
}

The problem is with onchange event I cannot call data attribute in my <select>. How could I call that data attribute inside function ?

6
  • You need to pass object of element as parameter Commented May 18, 2017 at 8:08
  • @MilindAnantwar Do you have another way to do that ? Commented May 18, 2017 at 8:09
  • Use jquery to bind change event Commented May 18, 2017 at 8:10
  • How could I do that ? Maybe you have some example ? @MilindAnantwar Commented May 18, 2017 at 8:10
  • Put id to the select and use $('#selectQTY').data('plan-type'); Commented May 18, 2017 at 8:11

3 Answers 3

2

You can use .bind()

The bind() method creates a new function that, when called, has its this keyword set to the provided value,

<select name="qty" class="mb10" onchange="test.bind(this)()" data-plan-type="offer" data-plan-id="7395" data-plan-day="2210009">
</select>

function test() {
  var this_plan_type = $(this).data('plan-type');
  var this_plan_id = $(this).data('plan-id');
  var this_plan_day = $(this).data('plan-day');
  console.clear();
  console.log(this_plan_type, this_plan_id, this_plan_day);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="qty" class="mb10" onchange="test.bind(this)()" data-plan-type="offer" data-plan-id="7395" data-plan-day="2210009">
    <option value="0">0 rooms</option>
    <option value="1">1 rooms</option>
    <option value="2">2 rooms</option>
    <option value="3">3 rooms</option>
    <option value="4">4 rooms</option>
</select>

Sign up to request clarification or add additional context in comments.

2 Comments

Wow. Interesting...wasn't aware of this sytax :)
It's working and @MilindAnantwar answer is working too. Thanks to you both.
1

Your code is not working because keyword this do not refers to select elements context, it is refering to document instead.

You can

1) either pass object of clicked element as argument in function.

and in js:

function test($selectElement)
{
var this_plan_type = $selectElement.data('plan-type');
var this_plan_id = $selectElement.data('plan-id');
var this_plan_day = $selectElement.data('plan-day');

alert(this_plan_type);
}

2) get element by selector in test method:

 function test()
{
var $selectElement = $("select.mb10")
var this_plan_type = $selectElement.data('plan-type');
var this_plan_id =  $selectElement.data('plan-id');
var this_plan_day = $selectElement.data('plan-day');

alert(this_plan_type);
}

3) Use jquery to attach event to select element:

$(".mb10").change(function(){
   var $selectElement = $(this);
   var this_plan_type =$selectElement.data('plan-type');
   var this_plan_id = $selectElement.data('plan-id');
   var this_plan_day = $selectElement.data('plan-day');
   alert(this_plan_type);
});

Comments

1

Pass the <select> as an argument to test() and use that instead of this (which is bound to the window object in your case).

But honestly, you shouldn't use the onchange attribute at all. jQuery's change() method should absolutely do the work, even in IE11.

function test(mySelect)
{
    var this_plan_type = $(mySelect).data('plan-type');
    var this_plan_id = $(mySelect).data('plan-id');
    var this_plan_day = $(mySelect).data('plan-day');

    alert(this_plan_type);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="qty" class="mb10" onchange="test(this)" data-plan-type="offer" data-plan-id="7395" data-plan-day="2210009">
    <option value="0">0 rooms</option>
    <option value="1">1 rooms</option>
    <option value="2">2 rooms</option>
    <option value="3">3 rooms</option>
    <option value="4">4 rooms</option>
</select>

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.