Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
Hi,
How to get "class" attribute of an HTML element.
For example, for this HTML code, this C# code does well:
hElement.GetAttribute("id")  // Results "target_validate"


But it does not work for "class" attribute:
hElement.GetAttribute("class")  // Results ""


I use C# WebBrowser control.

<table id="target_validate" class="res_target" cellpadding="1" cellspacing="1">
	<tr>
		<td class="vil" colspan="2">AVATAR (18|60)</td>
	</tr>
	<tr>
		<th>User:</th>
		<td><a href="spieler.php?uid=4018">Iman</a></td>
	<tr>
		<th>Time:</th>
		<td>0:31:49</td>
	</tr>
</table>
Posted
Updated 2-Aug-18 22:24pm
v2

user "className' instead of "class"

like GetAttribute("className")
 
Share this answer
 
Comments
Prathyangira's Karthikeyan 3-Sep-15 1:27am    
wow best answer i like that thankyou from Prathyangira`s Karthikeyan (WEb Desinger) india
Hey,

I have no idea why, and doesnt make sense with what is actually in the html, but instead of htmlelement.GetAttribute("class"), use htmlelement.GetAttribute("classname"). I have it implemented as such in my app and works perfect.
 
Share this answer
 
the following code will return "res_target"


alert(document.getElementById('target_validate').className);
 
Share this answer
 
Guess you are using some C# methods to access the HTML DOM. Would be useful to know which library you are using (is that built into .Net?). Anyway, I imagine this explains why it isn't working:

http://www.webmasterworld.com/javascript/3090396.htm

Basically, you should use "obj.className" rather than "obj.getattribute('className')". Not sure if there is a corresponding function on whatever class "hElement" is. If not, then perhaps you can call a JavaScript function that just returns the result of "obj.className". You'd embed that JavaScript function in the HTML page you are calling, then call that JavaScript function from C# (again, I'd have to know what library you are using).
 
Share this answer
 
hello
how to access to "display" style type (hide, block, ...) in webbrowser?
tnx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900