Solved

jQuery: Replace replace text to change id

Posted on 2010-09-24
10
671 Views
Last Modified: 2012-08-14
I want to change the id value using jQuery.  So I need to find
       id="  
and replace it with
       id="zoo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<style type="text/css">
#p1, #p2 {color: red}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
</head>
<body>

<p><a href="#" id="rq1">Click Here to change ID values</a></p>

<div id="leadform1">

<p id="p1">Hello</p>
<p id="p2">World</p>

</div>

<h1>Footer</h1>

<script type="text/javascript">
/* <![CDATA[ */

$(document).ready(function(){
 $('#rq1').click(function() {
  alert('I want to add "zoo" to the beginning of every id inside leadform1');
  return false;
 });
});

/* ]]> */ 
</script> 

</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
  • 4
  • 3
  • 3
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33753461
$("#leadform1 > *").each(function(){
   $(this).attr("id", ("zoo" + $(this).attr()) );
});
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33753463
You should not set the same id for multiple object. WIth an indice :
Use :


$(document).ready(function(){
 $('#rq1').click(function() {
  var i = 0; // indice to have distinct id
  $("p","#leadform1").each(function() { // only paragraph
//      alert('I want to add "zoo" to the beginning of every id inside leadform1');
      $(this).attr("id", "zoo"+i++);
//      return false;
  });
 });
});

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33753507
sorry it should be
$("#leadform1 > *").each(function(){
   $(this).attr("id", ("zoo" + $(this).attr("id")) );
});
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33753565
this worked for me
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<style type="text/css">
#p1, #p2 {color: red}
</style>
<script type="text/javascript" src="jquery-1.js"></script>
</head>
<body>

<p><a href="#" id="rq1">Click Here to change ID values</a></p>

<div id="leadform1">

<p id="p1">Hello</p>
<p id="p2">World</p>

</div>

<h1>Footer</h1>

<script type="text/javascript">
/* <![CDATA[ */

$(document).ready(function(){
 $('#rq1').click(function() {
  alert('I want to add "zoo" to the beginning of every id inside leadform1');
  $("#leadform1").children().each(function(){
	alert($(this))
    $(this).attr("id", "zoo" + $(this).attr("id") );
});
  return false;
 });
});

/* ]]> */ 
</script> 

</body>
</html>

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 33754225
Thank you both.  Unfortunately your ideas do not work for me because they are not recursive.  Please test the code below and you will see the problem, thanks!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="jquery-1.js"></script>

<style type="text/css">
#p1, #p2 {color: red}
</style>

</head>
<body>

<p><a href="#" id="rq1">Click Here to change ID values</a></p>

<div id="leadform1">

<div>
<p id="p1">Hello <span id="p2">World</span></p>

<form id="xyz" action="./" method="get">
<p>
<input type="text" name="foo" id="zooph" />
<label for="foo">Company Name:</label>
</p>
</form>

</div>
</div>

<h1>Footer</h1>

<script type="text/javascript">
/* <![CDATA[ */

$(document).ready(function(){
 $('#rq1').click(function() {
  var i=0;
  $("#leadform1").children().each(function(){
    var r = Math.floor(Math.random()*99);
    if($(this).attr("id"))  $(this).attr("id", "i" + r + "_" + (i) + $(this).attr("id"));
    if($(this).attr("for"))  $(this).attr("for", "i" + r + "_" + (i) + $(this).attr("for"));
    if($(this).attr("name") )$(this).attr("name", "i" + r + "_" + (i) + $(this).attr("name"));
});
  return false;
 });
});

/* ]]> */ 
</script> 

</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 33754291
Use .find() instead of .children() in this case : http://api.jquery.com/find/


$("#leadform1").find().each(function(){

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 33756168
Thanks, but how can I use find to find/replace everything?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="jquery-1.js"></script>

<style type="text/css">
#p1, #p2 {color: red}
</style>

</head>
<body>

<p><a href="#" id="rq1">Click Here to change ID values</a></p>

<div id="leadform1">

<div>
<p id="p1">Hello <span id="p2">World</span></p>

<form id="xyz" action="./" method="get">
<p>
<input type="text" name="foo" id="zooph" />
<label for="foo">Company Name:</label>
</p>
</form>

</div>
</div>

<h1>Footer</h1>

<script type="text/javascript">
/* <![CDATA[ */

$(document).ready(function(){
 $('#rq1').click(function() {
  var i=0;
  $("#leadform1").find().each(function(){
    var r = Math.floor(Math.random()*99);
    if($(this).attr("id"))  $(this).attr("id", "i" + r + "_" + (i) + $(this).attr("id"));
    if($(this).attr("for"))  $(this).attr("for", "i" + r + "_" + (i) + $(this).attr("for"));
    if($(this).attr("name") )$(this).attr("name", "i" + r + "_" + (i) + $(this).attr("name"));
});
  return false;
 });
});

/* ]]> */ 
</script> 

</body>
</html>

Open in new window

0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 250 total points
ID: 33756242
hmm...leak is right, find() is the recursive version of children()

This will find all descendants: $(this).find('*');

0
 
LVL 16

Author Closing Comment

by:hankknight
ID: 33773711
Thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33773718
You're welcome! Thanks for the points!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

726 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