Solved

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

Posted on 2008-06-11
3
1,492 Views
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 < > carrots.it creates an id attribute with the same name as name="objectname".  Anybody have any idea how to do this?
0
Comment
Question by:kcg-witchdoctor
3 Comments
 
LVL 3

Accepted Solution

by:
KhoiNqq earned 250 total points
ID: 21766240
Here the PHP code you needs
<?php

$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;

				break;

			}

			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

0
 
LVL 6

Assisted Solution

by:ysfx
ysfx earned 250 total points
ID: 21766309
Here's a js solution:
	<script>
 
 

		if(document.body.all)

		{

			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;

			}

		}

		else

		{

			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;
 

				}

			}

		}
 

	</script>

Open in new window

0
 
LVL 2

Author Closing Comment

by:kcg-witchdoctor
ID: 31466434
Thank you both, i wish i knew how to give 100000 points to yuou both.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now