سؤال

I mocked up some HTML to test this:

<input type="text" id="text" />
<select id="options">
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
    <option>one</option>
</select>

I was using the following JavaScript to alert the type of the select tag, but I kept getting false.

var elem = $("#text");
alert(elem.after().attr('id'));

I also tried using Jquery's prop() method but to no avail, I am just trying to check whether the element that follows the input tag is a select tag...

هل كانت مفيدة؟

المحلول

You need to use .next() since select is the immediate next sibling of your input with id text:

alert(elem.next().attr('id'));

Your code is not working because .after() is used to

Insert content, specified by the parameter, after each element in the set of matched elements.

نصائح أخرى

First you have to use next instead of after() also you have to use type attribute to check if the next element is Select, use prop instead of attr

Live Demo

alert(elem.next().prop('tagName') == 'SELECT');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top