views:

96

answers:

4
function ajaxCall(query){
$.ajax({
    method:"get",
    url:"/main/",
    data:"q="+query,
    beforeSend:function() {},
    success:function(html){
        $("#main").html(html);
    }
    });
};

This is the entire code that will populate #main:

<p>{{ num_results }}, you just searched for {{ query }}</p>

Suppose I have another div called $("secondary") ....how would I populate that with {{ num_results }}, which is a part of the code, instead of all of it?

A: 

Use a regular expression

success:function(html){
        $("#main").html(html);
        $("secondary").html( /.*?(?=,)/.exec(html) );
    }
Gaby
the javascript won't actually see the {{ }} from the django template.
Brandon H
@Brandon H, ok (i do not use django), updated regexp. it will select anything up to a comma (,)
Gaby
A: 
$('#secondary').html($('#main p').html().split(',')[0]);
Brandon H
+5  A: 

One option is to return json with the data you need for each area.

$.ajax({
    method:"get",
    url:"/main/",
    dataType: "json",
    data:"q="+query,
    beforeSend:function() {},
    success:function(json){
        $("#main").html(json.main);
        $("#secondary").html(json.secondary);
    }
});

What you would be returning is:

{
    "main": "<p>{{ num_results }}, you just searched for {{ query }}</p>",
    "secondary": "{{ num_results }}"
}
PetersenDidIt
+1  A: 

You will need to return a XML or JSON object from the request which will the be used to fill in only the relevant parts. Try something like this:

function ajaxCall(query){
$.ajax({
    method:"get",
    url:"/main/",
    data:"q="+query,
    beforeSend:function() {},
    success:function(html){
        $("#main").html(html.main);
        $("#secondary").html(html.secondary);
    }
    });
 };

You will also need to update the server-side to return a JSON object.

sttwister