Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Remove attribute from tags in var

Using jQuery, how can I remove all longdesc attributes from images within a var?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

var stuff='<p><a><img src="image.gif" longdesc="hello" alt="" /></a></p><p><a><img src="image.gif" longdesc="hello" alt="" /></a></p><img src="image.gif" longdesc="hello" alt="" />';
alert( $(stuff).find('img').removeAttr('longdesc').html()  );

// Should alert <p><a><img src="image.gif" alt="" /></a></p><p><a><img src="image.gif" alt="" /></a></p><img src="image.gif" alt="" />

});

</script>

</head>
<body>

</body>
</html>

Open in new window

Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

What exactly you are doing?? just passing image in variable I don't know but the easy way is using each and remove attribute see the example below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('img').each(function(){ 
		$(this).removeAttr('longdesc')
	});
});
</script>
</head>
<body>
<p><a><img src="image.gif" longdesc="hello" alt="" /></a></p>
<p><a><img src="image.gif" longdesc="hello" alt="" /></a></p>
<img src="image.gif" longdesc="hello" alt="" />
</body>
</html>
                                  

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of vuhanguyen
vuhanguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial