Search code examples
javascriptjquerymeta-tagsjquery-filter

in jQuery, why is the value of "this" when querying 'meta[name]' different from 'meta[http-equiv]' different?


Given:

<meta name="someKindOfId" value="asdf" />
<meta http-equiv="Set-Cookie" content="cookie_value1" />

and

var meta1 = $('meta[name]').filter(function() {
    console.log(this['name']);
});

var meta2 = $('meta[http-equiv]').filter(function() {
    console.log(this['http-equiv']);
});

The first console.log outputs someKindOfId (correct).

But the second console.log outputs undefined twice, instead of Set-Cookie?

Why is this so?


Solution

  • Because name is a property of dom element object where are http-equiv is not, it is an attribute value which is not copied as a property so try

    var meta2 = $('meta[http-equiv]').filter(function() {
        console.log($(this).attr('http-equiv'));
    });
    

    Attributes like name, id, value has corresponding property matches in the dom element reference, so those values will get copied to the properties of the dom element reference and this inside the filter refers to the dom element