Ad

TagName Returns Undefined

- 1 answer

I am running a loop on a number of elements and trying to access another set of elements using the ids I get in the loop I attempt to refer to other elements and get their tag, here's my code.

function checkRequired(){
    var i = 0;
    $(".required_div").each(function(index){
        if( $(this).html() != '')
        {
            var question_id = $(this).attr('id').substring(9);
            var question_element = $('[name="ry['+question_id+']"');

            console.log(question_element);
            console.log(question_element.tagName);
        }
    });
    console.log(i);
}

And this is what I get in the console for each element:

1. [textarea#mce_editor_4.tinymce, prevObject: jQuery.fn.jQuery.init[1], context: document, selector: "[name="ry[67]""]
2. undefined

I've also tried to access the tagName using prop as mentioned here but that didn't work as it returns question_element.prop is not a function(…).

Ad

Answer

It's returning undefined because question_element is a jQuery object.

You could either access a DOM element in the jQuery object, then get the property:

question_element[0].tagName

or you could use the .prop() method:

question_element.prop('tagName');
Ad
source: stackoverflow.com
Ad