Closed Thread
Thread Tools Search this Thread
Unread 16th Jul 2012, 04:06 AM   #1
CustomBotSolutions.com
War Room Member
 
kojakeugenio's Avatar
 
Join Date: 2007
Posts: 164
Thanks: 16
Thanked 21 Times in 19 Posts
Default
Execute a function when selected - javascript
Share on: 
fb share twitter share gplus share more share

How can achieve this. For example on this code:

Code:
<select name="">
  <option value="test1">test1</option>
  <option value="test2">test2</option>
  <option value="test3">test3</option>
</select>
When I select test2 it will trigger a function?

Please advise..

Thanks

Best Blog Commenting Software
Youlikehits ROBOT - Earn 15k+ points daily for FREE!
Contact me for your Custom Bot Needs - CLICK HERE!
kojakeugenio is offline  
Unread 16th Jul 2012, 04:19 AM   #2
HyperActive Warrior
War Room Member
 
Join Date: 2009
Posts: 156
Thanks: 1
Thanked 11 Times in 10 Posts
Default
Re: Execute a function when selected - javascript
Share on: 
fb share twitter share gplus share more share

Give the select object and id and in your javascript add something like:

var dothis = function(){
return function(e){
var s = document.getElementById('yourId');
if(s.options[s.selectedIndex]=='test2'){
// YOUR CODE HERE
}
}
}
document.getElementById('yourId').onchange = dothis();

kdavies is offline  
Unread 16th Jul 2012, 05:03 AM   #3
CustomBotSolutions.com
War Room Member
 
kojakeugenio's Avatar
 
Join Date: 2007
Posts: 164
Thanks: 16
Thanked 21 Times in 19 Posts
Default
Re: Execute a function when selected - javascript
Share on: 
fb share twitter share gplus share more share

Hi kdavies thanks! How can I execute that function if any value on the list is selected?

Best Blog Commenting Software
Youlikehits ROBOT - Earn 15k+ points daily for FREE!
Contact me for your Custom Bot Needs - CLICK HERE!
kojakeugenio is offline  
Unread 16th Jul 2012, 09:06 AM   #4
Advanced Warrior
War Room Member
 
Brandon Tanner's Avatar
 
Join Date: 2006
Location: USA
Posts: 1,907
Thanks: 236
Thanked 1,377 Times in 676 Posts
Default
Re: Execute a function when selected - javascript
Share on: 
fb share twitter share gplus share more share

Here's another way to do it...

Code:
<!-- Javascript -->

<script type="text/javascript">
     function test1() {
     alert("You chose 1!");
     }
     function test2() {
     alert("You chose 2!");
     }
     function test3() {
     alert("You chose 3!");
     }  
</script>


<!-- HTML -->

<select name="">
  <option value="test1" onclick="test1()">test1</option>
  <option value="test2" onclick="test2()">test2</option>
  <option value="test3" onclick="test3()">test3</option>
</select>


Brandon Tanner is offline  
Unread 16th Jul 2012, 09:18 AM   #5
HyperActive Warrior
War Room Member
 
John Ayling's Avatar
 
Join Date: 2009
Location: Australia
Posts: 225
Thanks: 40
Thanked 47 Times in 36 Posts
Default
Re: Execute a function when selected - javascript
Share on: 
fb share twitter share gplus share more share

And if you want to use jQuery..

Code:
<script type="text/javascript">
$(document).ready(function(){
     $('#mySelect').change(function(){
           var selectedOption = $(this).val();
           alert("You selected " + selectedOption);
      });
});
</script>


<select name="" id="mySelect"> 
     <option value="test1">test1</option> 
     <option value="test2">test2</option> 
     <option value="test3">test3</option> 
</select>

Software Marketing & Licensing System for WordPress Plugins, Themes & .NET Software
>> 72 Hour Special <<
John Ayling is offline  
Closed Thread


Bookmarks

Tags
execute, function, javascript, selected

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 06:50 AM.