Jon McIntosh
Jon McIntosh

Reputation:

Pass variables to jQuery function

so simple question! How can I pass a variable into a jQuery function, like in Javascript?

    <script type="text/javascript">
   function foo(bar) {
     $("#content").load(bar);
    }
    </script>
    <a href="#" onClick="foo('test.php');">Load</a>

The above code doesn't work, clearly, but hopefully you can get an idea for what I'm asking.

Upvotes: 1

Views: 5990

Answers (2)

Kobi
Kobi

Reputation: 137997

You are programming in JavaScript.
jQuery is just a library - a bunch of JavaScript functions you can use; it is not another language.

That said, a more modern way of achieving your goal is to use jQuery for event binding

<a href="#" id="LoadLink">Load</a>

.

function foo(bar) {
   $("#content").load(bar);
}

$(document).ready(function(){
  $("#LoadLink").click(function(){
    foo("test.php");
    return false; //link won't scroll to the top
  }); //click
});//ready

Working example: http://jsfiddle.net/wtL88/

Upvotes: 2

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

<a href="javascript:;" onclick="javascript:foo('test.php');">Load</a>

Upvotes: 0

Related Questions