Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending variables to a JQuery function

Tags:

jquery

I'm a novice in jQuery, but I hope somebody will be able to help me out. I have searched this forum (and others) but haven't been able to find an answer that I could make work.

I have a link like this:

<a href="#">

and a jQuery script:

$("div.show_dialogbox").click(function(){
    $("div#dialogboxwraper").animate({
        height: "400px"
    }, "slow")
    .animate({
        height: "200px"
    }, "slow");
});

I would like to be able to send a variable with the link and use it in the script for the height like this:

<a href="#" OnClick="variable(200)">


$("div.show_dialogbox").click(function(variable){
    $("div#dialogboxwraper").animate({
        height: variable+200+"px"
    }, "slow")
    .animate({
        height: variable+"px"
    }, "slow");
});
like image 290
Jesper Møller Avatar asked Sep 13 '26 14:09

Jesper Møller


2 Answers

I would use data attributes here, for which more support was added in jQuery 1.4.3, like this:

<a href="#" class="show_dialogbox" data-height="200">

Then in jQuery:

$("a.show_dialogbox").click(function(){
  $("#dialogboxwraper").animate({
    height: $(this).data('height') + 200
  }, "slow")
  .animate({
    height: $(this).data('height')
  }, "slow");
});

You can test it out here.

like image 102
Nick Craver Avatar answered Sep 16 '26 01:09

Nick Craver


Not sure about explicity sending a variable but you could set the title of the anchor to be "200px" and then inside of your function do something like

var h = $(this).attr('title');
like image 45
Aaron Hathaway Avatar answered Sep 16 '26 00:09

Aaron Hathaway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!