Jump to content

Recommended Posts

Hi, I am trying to get the background of a div to change when the user is typing in the textfield inside it... sadly I cannot get it to work. Could someone please help me solve this?

 

Thanks in advance...

 

Javascript

 

function changebg() {

var docEl = this.parentNode.getElementsByTagName('greenhighlight'); docEl.style.backgroundImage = "url('images/form-highlight-bg.jpg')";

}

 

 

css

 

.greenhighlight {float:left; width:980px; height:60px;}

 

 

html

 

<fieldset>

<div class="greenhighlight">

<span class="hint">The contact name is the name of the person who is to receive the enquiries for the adverts we submit. This field is required.</span>

<label for="contactname"><strong>* Contact Name:</strong></label>

<input type="text" id="contactname" onFocus="changebg(greenhighlight);"/>

</div>

</fieldset>

function changebg() {
var docEl = this.parentNode.getElementsByTagName('greenhighlight'); docEl.style.backgroundImage = "url('images/form-highlight-bg.jpg')";
}

You are searching for elements by TagName...

 

 

.greenhighlight	{float:left; width:980px; height:60px;}

greenhighlight is a css ClassName...

 

Searching by tag name is used to search for tags, i.e. divs or span's etc. You need to search for your class name, so change the function to say 'getElementsByClassName' and that should sort it.

 

Hope this helps,

Joe

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.