Jump to content

Recommended Posts

Posted

The code searches for occurances of

but i want it to replace:
does anyone know how the below javascript might be changed to do this?

 

/*
dan function by Roger Johansson, http://www.456bereastreet.com/
*/
var dan = {
init : function() {
// Check that the browser supports the DOM methods used
	if (!document.getElementById || !document.createElement || !document.appendChild) return false;
	var oElement, oOuter, oI1, oI2, tempId;
// Find all elements with a class name of dan
	var arrElements = document.getElementsByTagName('*');
	var oRegExp = new RegExp("(^|\\s)dan(\\s|$)");
	for (var i=0; i	// Save the original outer element for later
		oElement = arrElements[i];
		if (oRegExp.test(oElement.className)) {
// 	Create a new element and give it the original element's class name(s) while replacing 'dan' with 'cb'
			oOuter = document.createElement('div');
			oOuter.className = oElement.className.replace(oRegExp, '$1cb$2');
// Give the new div the original element's id if it has one
			if (oElement.getAttribute("id")) {
				tempId = oElement.id;
				oElement.removeAttribute('id');
				oOuter.setAttribute('id', '');
				oOuter.id = tempId;
			}
// Change the original element's class name and replace it with the new div
			oElement.className = 'i3';
			oElement.parentNode.replaceChild(oOuter, oElement);
// Create two new div elements and insert them into the outermost div
			oI1 = document.createElement('div');
			oI1.className = 'i1';
			oOuter.appendChild(oI1);
			oI2 = document.createElement('div');
			oI2.className = 'i2';
			oI1.appendChild(oI2);
// Insert the original element
			oI2.appendChild(oElement);
// Insert the top and bottom divs
			dan.insertTop(oOuter);
			dan.insertBottom(oOuter);
		}
	}
},
insertTop : function(obj) {
	var oOuter, oInner;
// Create the two div elements needed for the top of the box
	oOuter=document.createElement("div");
	oOuter.className="bt"; // The outer div needs a class name
    oInner=document.createElement("div");
    oOuter.appendChild(oInner);
	obj.insertBefore(oOuter,obj.firstChild);
},
insertBottom : function(obj) {
	var oOuter, oInner;
// Create the two div elements needed for the bottom of the box
	oOuter=document.createElement("div");
	oOuter.className="bb"; // The outer div needs a class name
    oInner=document.createElement("div");
    oOuter.appendChild(oInner);
	obj.appendChild(oOuter);
},
// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
addEvent : function(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}
};

dan.addEvent(window, 'load', dan.init);

Posted
I think you just need to change the references to class to their ID equivalents as the regular expression looks like it will find anything with "dan" as part of it.
Posted (edited)

As long as you don't want to change anything else - just change

 

if (oRegExp.test(oElement.className))

 

to

 

if (oRegExp.test(oElement.id))

 

All the outer loop does is to get an array of all the elements and go through them matching the className using the given regEx.

 

having said that it's only checking for Dan with optional spaces, not containing Dan, so if you are pretty sure you haven't got the spaces you could cut most of it out with Document.getElementById("Dan")

 

L8R: No it doesn't, wasn't looking carefully, it looks for the word Dan on its own !

 

Have you considered jQuery ?

Edited by autismuk
Corrected Mistake
  • Thanks 1
Posted

Autismuk - Thank you so much!

 

I dont know javascript and have heard of JQuery but think for the novice (me) it will be slightly beyond me lol

 

This has saved me so much bother!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...