[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

jQuery: Remove attribute from tags in var

Posted on 2013-06-09
2
Medium Priority
?
433 Views
Last Modified: 2013-06-11
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

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39233681
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

0
 
LVL 1

Accepted Solution

by:
vuhanguyen earned 2000 total points
ID: 39233803
If you just want to remove a string within a var, use this:


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.replace(/longdesc="\w*"/g,''));

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question