Reputation: 81
I've managed to get a table from another page into a variable called res
.
I need to pass this variable through a filter for search
.
I'm getting the table row that corresponds by using .parents()
.
This filter works when the table is on the same page as the jQuery, but not when using $.get()
.
Below is my approach:
$.get('oncalltable.html', function(res) {
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
var oncalldate = ((''+month).length<2 ? '0' : '') + month + '/' +
((''+day).length<2 ? '0' : '') + day + '/' + year % 100;
var search = oncalldate;
var todayoncall = $("span").filter(function() {
return $(this).text() == search;
}).parents('tr');
console.log(oncalldate, res, todayoncall);
$('.test span').append(todayoncall);
});
Upvotes: 0
Views: 680
Reputation: 2379
JQuery selectors work against the DOM. oncalltable.html isn't in DOM, it's just text inside a variable that happens to resemble HTML.
I'm guessing that you expect your $("span") selector to see the contents of oncalltable.html. It won't because the <span>
you're looking for (inside oncalltable.html) isn't in DOM.
If (and only if) you are supremely confident that the data you get in oncalltable.html is now and always will be completely trustworthy, you can add it to your DOM by loading that html into your page.
One way to do that would be to assign the contents of 'res' to the .innerHTML of some existing page element. If you had (or decided to make) a hidden div to hold your data while it's parsed:
<div id="whereResGoes" style="display:none"></div>
then in the function you pass to $.get (just above the part where you use the $('span') selector), run
$('#whereResGoes').html(res);
Now it's loaded into your DOM and your $('span') selector can see it.
Note that since this approach makes oncalltable.html part of your page, it's like letting the author of oncalltable.html write anything they want on your page. If that page is ever compromised, yours will be too.
Upvotes: 2