0

I am having variable .i.e. var gtval it return a image tag and now i want the fetch the class name of that img tag;

var gtval = aData[4];
alert(gtval);

alert display-

    <img id="ctl00_ContentPlaceHolder1_gv_Image1" class="del_10 pointer" 
src="images/delete_item.png" style="border-width:0px;">

I want to fetch del_10 value

Satinder singh
  • 10,100
  • 16
  • 60
  • 102

5 Answers5

2

Could be just:

alert($(gtval).attr('class').split(' ')[0]);

To be sure older browsers will support it:

alert($($.trim(gtval)).attr('class').split(' ')[0]);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1
var classList = gtval.attr('class').split(/\s+/);

Return list of used classes.

okuznetsov
  • 278
  • 1
  • 5
  • 12
1

If you are trying to get the value "del_10", Does the value have to be a className for starters?

<img id="ctl00_ContentPlaceHolder1_gv_Image1" rel="del_10" class="pointer" src="images/delete_item.png" style="border-width:0px;">

alert( $('#'+aData[4]).attr('rel') );

Otherwise the other answers given above should work. Though I imagine you need to inclode the hash as so:

alert( $('#'+aData[4]).attr('class').split(/\s+/)[0]; );
Marc
  • 5,109
  • 2
  • 32
  • 41
1

You can do this:

var className = $(gtval).map(function () {
    return this.className;
})[0].split(' ').join(',');

console.log(className); // result: del_10,pointer 
palaѕн
  • 72,112
  • 17
  • 116
  • 136
1

or you could get the raw DOM and get the classList

 $(gtval)[0].classList