jqueryselectattributesnodename

jQuery can't select element by attribute name and get the nodename


I am trying to select this element by attribute name and print the nodename, but the result is undefined.

The first alert works, but the second alert doesn't work.

HTML code:

<objeto bordeactivo type="image">

  <img src="https://picsum.photos/536/354"/>

</objeto>

jQuery code:

var ob = $("[bordeactivo]");
var ob2 = ob.find("img");

// Works!
alert("ob>"+ob.attr("type"));

// NOT WORKING!
alert("nodeName>"+ob2.nodeName);

What is the problem?

My JsFiddle:

jsfiddle code sample


Solution

  • By using ob2.prop("nodeName"), you can access the DOM property nodeName of the underlying <img> element and display it in the alert box.

    var ob = $("[bordeactivo]");
    var ob2 = ob.find("img");
    
    // Works
    console.log("ob>" + ob.attr("type"));
    
    // Updated code
    console.log("nodeName>" + ob2.prop("nodeName"));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <objeto bordeactivo type="image">
        <img src="https://picsum.photos/536/354" />
    </objeto>