Another 'this' question!

Another ‘this’ question !

Hi all

Sorry folks it’s another ‘this’ question, I am looking for answers but I’m stuck again.

I have a group of drop down list that all call a function. In the function I just want to capture
the value that has been selected.

I’m always getting the first value because I need to capture ‘this’ value.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns="" xml:lang="en" lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<script src=""></script>
	<style type="text/css">

  <div id="wrap">
    <form method="" action="">
      <select class="list" onchange="getList();">
        <option value="One">One</option>
        <option value="Two">Two</option>
        <option value="Two">Three</option>
        <option value="Two">Four</option>
      <select class="list" onchange="getList();">
        <option value="Two-One">Two-One</option>
        <option value="Two-Two">Two-Two</option>
        <option value="Two-Two">Two-Three</option>
        <option value="Two-Two">Two-Four</option>
      <select class="list" onchange="getList();">
        <option value="Three-One">Three-One</option>
        <option value="Three-Two">Three-Two</option>
        <option value="Three-Two">Three-Three</option>
        <option value="Three-Two">Three-Four</option>

  <script type="text/javascript">

    function getList(){
      //alert($(this, '.list').val());




It’s the inline HTML events that are getting in your way here.

<select class="list"[s][color="red"] onchange="getList();"[/color][/s]>
<select class="list"[s][color="red"] onchange="getList();"[/color][/s]>
<select class="list"[s][color="red"] onchange="getList();"[/color][/s]>

$('select.list').change(function () {

Keep your scripting out of the HTML content code, and you will be fine.

pass ‘this’ in each of the function calls, then change your function to this (no pun intended):

function getList(el){

Or you can drop the inline onchange in the html and do this:

$('.list').on('change', function() {

Is there a benefit to using $(this).val() over this.value ?

What possible benefit can using the jQuery accessors provide in this situation?

If I was using vanilla JS, then I would use this.value, but in using jQuery, I would use the jQuery way $(this).val() so I can use jQuery methods if I needed to.

The purpose of using jQuery is to serve as a concise library that simplifies things. I don’t see how $(this).val() is a simplification of this.value
Just because a technique for it exists, doesn’t mean that the technique is better or should be used.

Having said that though, there are times when $(…).val() is appropriate, but given that the this keyword is already available not as a jQuery object, it seems needless to convert the this keyword in to a jQuery object so that you can then retrieve the jQuery val() property to obtain the value of the jQuery object that used to be the this keyword.

Using this.value avoids all of those details, with no negative consequences.

I don’t disagree with you, and to be honest, I have to digress the answer because I was more answering for $(this) and not $(this).val() per say.

Thanks paul_wilkins, that makes more sense now.

So should I always keep scripting out of the HTML?

Your HTML content should be kept as free as possible from having CSS or JavaScript mixed in with it.
So that’s a yes.