0

I have made a function to get the variable of onclick and pass it to the function, but I want it in different format:

$('.ajax').live('click', function() {   
    var Loading = 'Loading...';
    var url = $(this).attr('href');
    var container = '#myDivcontent';
    looadContents(url,Loading,container);
    return false;
});

and the function looks like :

looadContents(url,Loading,container) {
..
...
$(contaner).load(url);
...
..
}

I would like to have like array format or json data format when I call the function:

$('.ajax').live('click', function() {   
    looadContents({
             url : $(this).attr('href'),
             Loading : 'Loading...',
             container: '#myDivcontent'
     });
    return false;
});

Any idea how can I do this?

4
  • Just to check: Is looadContents supposed to be the same function as ajaxModalLoad? Commented Jan 30, 2012 at 15:14
  • 1
    You will have to explain this a little further Commented Jan 30, 2012 at 15:15
  • what data do you want in json format ? Commented Jan 30, 2012 at 15:15
  • Retagged javascript; the question really has little to do with jQuery. Commented Jan 30, 2012 at 15:20

5 Answers 5

2

If you pass the information like an object, you can simply access it as one:

function looadContents(options) {
    $(options.container).load(options.url);
}

looadContents({ url: '...', container: '#...' });
Sign up to request clarification or add additional context in comments.

Comments

1

This syntax works as expected.

function display(obj)
{
    console.log(obj.name + " loves " + obj.love);
}
display({name:"Jared", love:"Jamie"});

Comments

1

Is this possibly what you are looking for?

function looadContents(o) {

  alert(o.url);
  alert(o.container);

}

looadContents({ url:"abc", container: "def" });

Comments

1

Use what's sometimes called an "options" object (what you refer to as "JSON data format" is simply a JavaScript object literal):

function loadContents(options) {
    var url = options.url;
    var Loading = options.Loading;
    /* etc. */
}

You can also leverage this approach to provide default values for certain parameters:

function loadContents(options) {
    var url = options.url || "http://default-url.com/",
    var Loading = options.Loading || "Loading...";
    var container = options.container || "#container";

    $(container).load(url);
}

Comments

0

Try this

looadContents(input) {
  //...
  $(input.contaner).load(input.url);
  //...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.