Will
Will

Reputation: 37

JavaScript <div> .class Extraction

I am writing JavaScript that retrieves a webpage from another site, and only displays 'Notices' from a site.
Fortunately, all 'Notices' are div elements of class 'event'.
I want to extract only these divs from the returned code so that I can re-format and display them. All the code I have so far is working, but I'm not sure on how to extract the 'event' divs from the source code. Any ideas?

function getNotices(){
    // Get the date from the form
    var str = document.getElementById('formDate').value; 
    var year = str.slice(1,4); // Extract Year
    var month = str.slice(6,7); // Extract Month
    var day = str.slice(9,10); // Extract Day

    // Inject correct date into URL
    var link = "<a href=\"http://ilearn.stpauls.school.nz/calendar/view.php?view=day&course=1&cal_d=" + day + "&cal_m=" + month + "&cal_y=" + year + "\">Raw Link</a>";
    // Write raw link to div for debugging
    document.getElementById('rawLink').innerHTML = link;  (debugging)

    // Bounce off anyorigin.com to get the source
    // Re-inject date into new link
    var anyLink = "http://anyorigin.com/get?url=http%3A//ilearn.stpauls.school.nz/calendar/view.php%3Fview%3Dday%26course%3D%26cal_d%3D" + day + "%26cal_m%3D" + month + "%26cal_y%3D" + year + "&callback=?"; // Splice the date into the school link and site to bounce it off
    $.getJSON('http://anyorigin.com/get?url=http%3A//ilearn.stpauls.school.nz/calendar/view.php%3Fview%3Dday%26course%3D1%26cal_d%3D30%26cal_m%3D7%26cal_y%3D2013&callback=?', function(data){
        var obj = JSON.stringify(data); // Turn object into a string
        document.getElementById('hopefullyTheData').innerHTML = obj; // Print string containing SPC website onto page (debugging)
    });
}

Thanks guys!

Upvotes: 1

Views: 213

Answers (3)

Ja͢ck
Ja͢ck

Reputation: 173572

You can append only the event nodes by searching inside data.contents:

$('.event', data.contents).appendTo('#hopefullyTheData');

Upvotes: 0

noj
noj

Reputation: 6759

Load the contents into jQuery then select by event class:

var events = $(data.contents).find('.event');

Upvotes: 1

schnill
schnill

Reputation: 955

use $('div.event') selector to extract all the div element having class event.

Upvotes: 0

Related Questions