-1

I want to call a function on button click with a dynamic parameter passed to that function.

HTML:

<button onclick=getvalue(val['id']):void(0);>Click</button>

JavaScript:

function getvalue(id) {
    alert(id);
}
Hitesh Chauhan
  • 87
  • 2
  • 4
  • 13

3 Answers3

7

You need to put the attribute in quotes because there are quotes in the value. Also, you have a syntax error with :void(0); after the function call.

<button id="button1" onclick="getvalue(this.id);">Click</button>

Or you can give the element an ID and bind the click handler in jQuery.

<button class="mybuttons" id="button1">Click</button>

<script>
$("#mybutton").click(function() {
    getvalue(this.id);
});
</script>
Amar Singh
  • 5,464
  • 2
  • 26
  • 55
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Refer this.....

<script type="text/javascript">
    function display(el) {
        var id = $(el).attr('id');
        alert(id);
    }
</script>

<input id="btn" type="button" value="click" OnClick="display(this);" />

How to pass parameter to click event in Jquery

Community
  • 1
  • 1
Wenson
  • 235
  • 4
  • 17
0

i did that it was a tiny mistake

<a href="#" class="pull-right button uppercase" onclick="getvalue('+val['id']+');">Click</a>

function getvalue(val){

     alert(val);
}

one more thing if you don't want to load your page just replace # > javascript:void(0) in href .

Hitesh Chauhan
  • 87
  • 2
  • 4
  • 13