0

I want get the id of the element that is clicked and within a specified class, and want to print the ID.

Here is my JS , I am really new JS , Please help

      $('.wrapinner').click(function(e) {
         $('.wrapinner').css("margin-top","0");
         $('.wrapinner').css("opacity","0.4");
         $(this).css("margin-top","25px");
         $(this).css("opacity","1");
         var r= document.getElementById(this).attributes.toString();
        document.write(r);
    });
CleanX
  • 972
  • 3
  • 16
  • 23

10 Answers10

4

There are two ways to do this:

1) Done without jQuery object (it is faster)

$(".wrapinner").click(function(){
  //Your Code
  var id = this.id;
  document.write(id);
});

OR

2) Done with jQuery object:

$(".wrapinner").click(function(){
  //Your Code
  var id = $(this).attr('id');
  document.write(id);
});

NOTE after jfriend00's comment.

Use document.write() if really needed and is not harmful to your page OR use console.log(). Read Why is document.write considered a "bad practice"?

Community
  • 1
  • 1
JERRY-the chuha
  • 584
  • 4
  • 16
  • 1
    Please use `console.log()`, not `document.write()` because `document.write()` will clear the entire document. – jfriend00 Dec 03 '13 at 07:42
3

You can call the native javascript object directly; this.id.

$('.wrapinner').click(function(e) {
     $('.wrapinner').css("margin-top","0").css("opacity","0.4"); // chained these two
     $(this).css("margin-top","25px").css("opacity","1"); // chained these two
     var r = this.id; // this should already reference the correct element.
    document.write(r);
});
Joel Peltonen
  • 11,167
  • 4
  • 60
  • 93
  • Eeeck. Don't use `document.write()` because that will clear the entire document. If it's just for debugging, use console.log() – jfriend00 Dec 03 '13 at 07:41
  • @jfriend00, I used `document.write()` because the OP used it in their function and I didn't want to guess what they actually wanted to do :) – Joel Peltonen Dec 03 '13 at 10:33
1

Try this way :

$('.wrapinner').click(function(e) {    
  console.log(this.id);
});
Ankit Tyagi
  • 2,327
  • 8
  • 19
0

You are not passing id to getElementById, rather you are passing the object it self. You can get id of source object of event using this.id not by this.

Change

var r= document.getElementById(this).attributes.toString();

To

var r= document.getElementById(this.id).attributes.toString();
Adil
  • 139,325
  • 23
  • 196
  • 197
0

Just write

document.write(this.id)
tewathia
  • 5,918
  • 3
  • 20
  • 27
  • I'm not advising him to use `document.write`. He's already using it in his code. The point of my answer was to mention that `this.id` would do the trick. – tewathia Dec 03 '13 at 07:32
0
$(".wrapinner").click(function(){
  var id = this.id
});
akash
  • 2,012
  • 3
  • 18
  • 31
0

Try this:

$('.wrapinner').click(function(e) {
         $('.wrapinner').css("margin-top","0");
         $('.wrapinner').css("opacity","0.4");
         $(this).css("margin-top","25px");
         $(this).css("opacity","1");
         var id = $(this).attr('id');
    });
Ringo
  • 3,436
  • 2
  • 20
  • 36
0

Try this example:

<table>
<tbody>
<tr id="CustomerScreen" class="rows"></tr>
<tr id="TraderScreen" class="rows"></tr>
<tr id="DistributorScreen" class="rows"></tr>
</tbody>
</table>
<script>
$('.rows').each(function () {
    var ar = this.id;
    console.log(ar);
});
</script>
Mr.G
  • 3,023
  • 2
  • 14
  • 19
0

You can check this code..

$('.wrapinner').click(function(e) {    
var getID = $(this).attr('id');
alert(getID);
});
Pradeep Pansari
  • 1,287
  • 6
  • 10
0

I am capturing click event on class and then finding its id and class name using parent(). demodemo

$(document).ready(function(){
    $('.class-first ul li, .class-second ul li, .class-third ul li').on('click',function(){
        console.log("child :"+$(this).attr('id') + " Parent:"+$(this).parents().eq(1).attr('class'));
    });
})
Prasad
  • 1,565
  • 4
  • 20
  • 25