webForumDet fria alternativet

this.name (??)

2 svar · 299 visningar · startad av echoSwe

echoSweMedlem sedan nov. 20041 189 inlägg
#1

Newbie fråga

<dl>
<dt><label for="txtName">Name:</label></dt>
<dd><input name="txtName" type="text" id="txtName" title="Your name" onfocus="colour_background(this.name, 0);" onblur="colour_background(this.name, 0);" /></dd>
</dl>

If you look at the function colour_background() - I'm trying to send the current name to be able to referr to the element from my function.

(which looks like this)

//forms script
function colour_background(object, formNumber) {
	var active;
	if (active) 
	{	
		document.forms[0].object.style.backgroundColor = "#FFF7DF"; 
		active = true;
	}
	else
	{
		document.forms[0].object.style.backgroundColor = "#fff"; 
		active = false;
	}
}

it would be nice to be able to loop through all the input elements on the page and place a call for this function in all "onfocus" and "onblur" attributes. Does anyone know how to accomplish this?

oj, skrev på engelska ;)... ni fattar.

tack på förhand.
//Henke

Hydr0maticMedlem sedan sep. 2001118 inlägg
#2

Du behöver inte använda this.name för att referera till objektet eftersom "this" är en direkt referens.
"active" variabeln är lokal så du kommer alltid hamna i else-satsen.

function colour_background(object) {
	object.style.backgroundColor = (object.style.backgroundColor == "#FFF7DF") ? "#fff" : "#FFF7DF"; 
}

.. så kalla på den med colour_background(this) bara.

För att sätta alla inputs onfocus/onblur, läs på om getElementsByTagName(), som om jag inte minns fel returnerar en NodeList som du kan loopa igenom.

echoSweMedlem sedan nov. 20041 189 inlägg
#3

Tackar :) jag ska sätta mig ner och kolla på det imorgon när jag har fått lite sömn.

129 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
126 ms — hämta tråd, inlägg och bilagor (db)