LoginSignup
38
38

More than 5 years have passed since last update.

jQueryで要素名を取得する

Last updated at Posted at 2014-04-04

たまに、使う。

sample
<inupt id="hello" type="text" />
<inupt class="hoge" type="text" />


<select class="hoge">
    <option>aaa</option>
</select>
console.log( $("#hello").prop("tagName") ); 
> "INPUT"

console.log( $(".hoge").prop("tagName") );
> "INPUT" //先頭しか取れない

console.log( $(".hoge").get(0).tagName );
> "INPUT"
console.log( $(".hoge").get(1).tagName );
> "SELECT"


/*
 * 下記は、冗長な書き方になっていると指摘いただきました。
 * 詳しくはコメント欄を参照ください
 */

//eachで回す 
$(".hoge").each(function(){ 
    console.log( $(this).prop("tagName") ); 
});
> "INPUT"
> "SELECT"


//mapで集める
var tagNameArray = $(".hoge").map(function(){
    return $(this).prop("tagName"); 
});
console.log( tagNameArray );
> ["INPUT", "SELECT"]

38
38
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
38