Tuesday 15 July 2014

javascript - how to pass dropdown selected value in jquery plugin -



javascript - how to pass dropdown selected value in jquery plugin -

i need pass dropdown selected value jquery plugin.

html: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="jquery.requiredplugin.js"></script> </head> <body> <select name="type" id="sid"> <option value="value1">value1</option> <option value="value2" selected>value2</option> <option value="value3">value3</option> </select> <textarea name="source" id="src1" cols="150" rows="20"></textarea> <script type="text/javascript"> $("#src1").keyup(function(e) { $(this).pluginmethod(e.keycode); }); </script> </body> </html> jquery.requiredplugin.js: (function ($) { // jquery plugin definition $.fn.pluginmethod = function (keycode) { homecoming this.each(function () { var $str = $('#src1'); var $soml = $('#sid'); var somlan = $soml.val(); //if assign var somlan static value code works var som = $str.val(); /* code goes here utilize both var som , var somlan*/ }); }; })(jquery);

i able value of $('#src1') , if assign var somlan static value code works. if want utilize dropdown selected value(whose id "sid" ) , retrieve value in plugin var $soml = $('#sid'); code doesn't work.

how can utilize dropdown selected value in plugin?

try : modify plugin method 2 argument instead of 1 , pass value of select box sec argument.

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript" src="jquery.requiredplugin.js"></script> </head> <body> <select name="type" id="sid"> <option value="value1">value1</option> <option value="value2" selected>value2</option> <option value="value3">value3</option> </select> <textarea name="source" id="src1" cols="150" rows="20"></textarea> <script type="text/javascript"> $("#src1").keyup(function(e) { $(this).pluginmethod(e.keycode,$('#sid').val()); }); </script> </body> </html>

plugin

(function($) { // jquery plugin definition $.fn.pluginmethod = function(keycode, selectval) { homecoming this.each(function() { var $str = $('#src1'); //var $soml = $('#sid'); var somlan= selectval; var som= $str.val(); /* code goes here utilize both var som , var somlan*/ }); }; })(jquery);

javascript jquery jquery-plugins

No comments:

Post a Comment