2

I want to pass a value on onclick event of a button so that javascript may update the value of hidden field. But I am unable to pass string to js function

JS

<script>
   function sethdnfiletype(filetype)
   {
       $("#hdnfiletype").val(filetype);
   }
</script>

HTML

<asp:ImageButton ID="btn1" runat="server" ToolTip="Upload Report"  OnClick="sethdnfiletype(// here I want to send a number like 1 or 2 or 3 //);" href="#uploadTOEFilePopup" data-toggle="modal" ClientIDMode="Static">
Tobias Brösamle
  • 598
  • 5
  • 18

2 Answers2

0

You can send values to a function using onclick, just pass them in as you would call a function usually.

function clickHandler(amount) {
  alert('Clicked with ' + amount);
}
<button type="button" onclick="clickHandler(123);">Click me</button>
Joey Ciechanowicz
  • 3,345
  • 3
  • 24
  • 48
  • Crazy thing is I am using the same code like you but mine is showing this error :CS1026: ) expected I seriously dont know why is it expecting closing bracket here – Faryal Imam May 11 '16 at 09:23
  • 1
    oh it was happening because i was using onclick event rather thatn using onclientclick – Faryal Imam May 11 '16 at 11:11
0

You can use bind. Bind allows you to set some default parameters and the this context. So you could do this: sethdnfiletype.bind(this, 1); which would pass in the value 1 to your function as the first parameter and the event as the second parameter.

winhowes
  • 7,845
  • 5
  • 28
  • 39