Pass parameter(s) to click event in jQuery -
i have html page below given code in corresponding .js file. have textarea element txtexamplereviewed
in when click alt+shift+a buttons, click event of btnaddmicroelement
gets fired , appends new div page label, text input , button inside it. text input in dynamically added div shall have text selected in textarea txtexamplereviewed
prior triggering event. have pass text click event of btnaddmicroelement
. have done given below not working , getting text microelement in text input instead of selected text textarea. please me correct code.
$(document).ready(function(){ $("#btnaddmicroelement").click(function(microelement){ alert("clicked"); $("#divverification").append("<div class='clsdivmicroelement'><label>microelement</label><input type='text' class='clstxtmicroelement'/ value=microelement><input type='button' class='clsbtnmicroelmentadd' value='add'/></div>"); return false; }); }); function getselectedtext(el) { var sel, range; if (typeof el.selectionstart == "number" && typeof el.selectionend == "number") { return el.value.slice(el.selectionstart, el.selectionend); } else if ( (sel = document.selection) && sel.type == "text" && (range = sel.createrange()).parentelement() == el) { return range.text; } return ""; } window.onload = function() { document.getelementbyid("txtexamplereviewed").onkeydown = function(e) { if(e.altkey && e.shiftkey && e.which==65){ $("#btnaddmicroelement").trigger("click",[getselectedtext(this)]); } }; };
custom parameter comes second argument, have fixed bit code:
$("#btnaddmicroelement").click(function (microelement, text) { $("#divverification").append("<div class='clsdivmicroelement'><label>microelement</label><input type='text' class='clstxtmicroelement'/ value="+text+"><input type='button' class='clsbtnmicroelmentadd' value='add'/></div>"); return false; });
please fiddle if need.
Comments
Post a Comment