javascript - tagName returns undefined - Stack Overflow

I am running a loop on a number of elements and trying to access another set of elements using the ids

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744798740a4594363.html

相关推荐

  • javascript - tagName returns undefined - Stack Overflow

    I am running a loop on a number of elements and trying to access another set of elements using the ids

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信