using href attribute as parameter in grep
Im trying to use the href attribute of a link to filter a json array with grep.
The grep works fine when the href value is hard coded into the grep parameter, (ie, el.category_name.toLowerCase() === "y"), but it doesnt work when I try to add the href dynamically as a variable from the button's href arrtibute.
I think it might be a question of scope... but Im not sure how to set up the variable.
Any help would be very much appreciated.
This is what I've been trying --
$('#categ开发者_开发知识库ory_name').click(function()
{
var href = $(this).attr('href');
$.getJSON('data/all.json',
function(data)
{
location = $.grep(data, function(el, i)
{
return 'el.' + href + '.toLowerCase() === "y"'
});
//returned grep-ed objects mapped to html//
});
});
Try this
$('#category_name').click(function()
{
var href = $(this).attr('href');
$.getJSON('data/all.json',
function(data)
{
location = $.grep(data, function(el, i)
{
return el[href].toLowerCase() === "y"'
});
//returned grep-ed objects mapped to html//
});
});
精彩评论