Ivan Rutter
Ivan Rutter

Reputation: 11

Passing in name/id of target div

Is it possible to pass in to a $.ajax function the name or id of the target div?

function getData(someurl, somedata, somediv){
    $.ajax({
        url:someurl,
        data:somedata,
        success:function(msg){
            $('#id_of_div').show().html(msg);
        }
    });
}

Have been able to pass in someurl and somedata but as yet unable to pass in the id of the target div.

How should the target div id be passed in?

Upvotes: 0

Views: 634

Answers (3)

mike
mike

Reputation: 8141

Use the context property...

function getData(someurl, somedata, somediv) {
    $.ajax({
        url: someurl,
        data: somedata,
        context: document.getElementById(somediv),
        success: function(msg) {
            $(this).show().html(msg);
        }
    });
}

getData('example.com', { some: data }, 'divId');

Upvotes: 1

joekarl
joekarl

Reputation: 2118

If somediv is a dom element or jquery object then you can do:

$(somediv).show().html(msg);

If somediv is the id of the element, then you can do what @boca said:

$('#' + somediv).show().html(msg);

An example call for choice one would be

getData("url", {d1:"",d2:""}, $('#messageDiv'));

And an example call for choice two would be

getData("url", {d1:"",d2:""}, 'messageDiv');

Upvotes: 0

boca
boca

Reputation: 2352

try

$('#' + somediv).show().html(msg);

Upvotes: 0

Related Questions