Raj Adroit
Raj Adroit

Reputation: 3888

JavaScript pass argument this.value to function

Here is my javascript code, i want to pass current selected value of the option to my js function, in this code i used static number 6.

<select name='project_name' class='required input_field' id='project_name' onchange="sendRequest('GET','getClientName.jsp?ProjectId=6')">
<option value=''>-- Select --</option>
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
</select>

help me to solve this...

Upvotes: 1

Views: 32000

Answers (3)

kannanrbk
kannanrbk

Reputation: 7134

It will help you to get the selected option value . Try this link http://jsfiddle.net/xyaaa/9/.

<html>
     <head>
         <script>
          function getOption(t){
               var val;
                var options = t.options;
                for(var i=0,len=options.length;i<len;i++){
                     var option = options[i];
                     if(option.selected){
                         val = option.value;
                     }
                }
                return val;
            }
             function  sendRequest(method , url){
                   console.log(url);
             }
    </script>
    </head>
            <body>
                <select onchange="var x=getOption(this);sendRequest('GET','getClientName.jsp?ProjectId='+x)">
                    <option value="1">Project 1</option>
                    <option value="2">Project 2</option>
                    <option value="3">Project 3</option>
                </select>
    </body>
</html>

Upvotes: 0

richoffrails
richoffrails

Reputation: 1033

var selectBox = document.getElementById('project_name');
selectBox.addEventListener('change', function() { 
  sendRequest('GET', 'getClientName.jsp?ProjectId=' + this.value); 
});

Upvotes: 2

Marc
Marc

Reputation: 6771

Change the string 'getClientName.jsp?ProjectId=6' to

'getClientName.jsp?ProjectId=' + this.options[this.selectedIndex].value)

or

'getClientName.jsp?ProjectId=' + this.value)

but I think the first one is more browser compatible.

Upvotes: 7

Related Questions