javascript to add id attribute that is the same as name attribute

Posted on 2008-06-11
Last Modified: 2012-06-27
I need a simple PHP or javascript script that parses an html page returned as a string, when it finds a name tage that doesn't have an id tag between the < > creates an id attribute with the same name as name="objectname".  Anybody have any idea how to do this?
Question by:kcg-witchdoctor
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

Accepted Solution

KhoiNqq earned 250 total points
ID: 21766240
Here the PHP code you needs
$str = "<html><body><div name=\"something\"><div name=\"something2\" id=\"test\"></div></div></body></html>";
$re  = "/<([^\s>]+) ([^>]+)>/";
if (preg_match_all($re, $str, $match)){
	foreach ($match[2] as $k => $attr_str){
		$attrs = explode(" ", $attr_str);
		$id_found = false;
		$name     = "";
		foreach ($attrs as $attr){
			if (substr($attr, 0, 3) == "id="){
				$id_found = true;
			if (substr($attr, 0, 5) == "name="){
				$name = substr($attr, 5);
				echo "Found name:" . $name;
		if (!$id_found && $name != ""){
			$current_tag = $match[0][$k];
			$new_tag = substr($current_tag, 0, -1) . " id=" . $name . ">";
			$str = str_replace($current_tag, $new_tag, $str);
echo "Result:\n" . $str;

Open in new window


Assisted Solution

ysfx earned 250 total points
ID: 21766309
Here's a js solution:
			for(i in document.body.all)
				if(document.body.all[i].name != "" && document.body.all[i].id == "")
					document.body.all[i].id = document.body.all[i].name;
			for(f in document.forms)
				for(e in document.forms[f].elements)
					if(document.forms[f].elements[e].name != "" && document.forms[f].elements[e].id == "")
						document.forms[f].elements[e].id = document.forms[f].elements[e].name;

Open in new window


Author Closing Comment

ID: 31466434
Thank you both, i wish i knew how to give 100000 points to yuou both.

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

630 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