admin管理员组

文章数量:1406760

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(…).

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(…).

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Jan 4, 2016 at 2:20 Naguib IhabNaguib Ihab 4,5068 gold badges51 silver badges86 bronze badges 2
  • 1 try question_element[0] – YarGnawh Commented Jan 4, 2016 at 2:31
  • @YarGnawh that worked, so $('name=...) gets multiple elements? – Naguib Ihab Commented Jan 4, 2016 at 2:33
Add a ment  | 

1 Answer 1

Reset to default 6

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');

本文标签: javascripttagName returns undefinedStack Overflow