How to work with an object in JS? I get the object in this way:

$(document).ready(function() { $('.button').on('click', function() { var Test = $(this).parents('div').find('a'); }); }); 

In order to display text() use the console.log(Test.text()); there is no problem with this, but when I try to output href , it displays undefined console.log(Test.href);

  • one
    console.log(Test[0].href); console.log(Test.attr("href")); - Igor

2 answers 2

The jQuery function (it is also $ ) returns a javascript wrapper object around a set of (possibly empty) DOM elements. This wrapper has methods and properties described in the jQuery-API.

href is a property of the DOM element with the a tag. You can reach it like this:

 console.log(Test[0].href); console.log(Test.attr("href")); 

    I add that the property href refers to those properties that do not always coincide with the attribute of the tag. According to the W3C specification , the href property should be a full reference. Therefore, if you need exactly what was written in the tag attribute, you should use the getAttribute method.

    For example, for links placed on the page at http://example.com

    <a href='/index.html'>Home</a>` console.log(test[0].href) // 'http://example.com/index.html' console.log(test[0].getAttribute('href')) // '/index.html'