Jump to content

Very newbie question - about clicking on a square and having it change colour


cliftonbazaar

Recommended Posts

Hi all,

 

Sorry about the noob question but I have never programmed in Javascript (PHP and HTML  :D ).

 

I wish to have squares (3 wide by 3 deep), and when I click on a square the background colour changes (without reloading the page).  Can this be done in Javascript?  and if so where should I go to learn how to do this?

 

Please don't post the answer as I wish to learn how to do it (if it can be done).  Just a simple link to a page that shows me how to do it is fine.

 

James

Link to comment
Share on other sites

This is how I would approach your problem, it might not be the right way, or the most efficient way, but it will be a start:

 

Make each square a submit button for a form.

Add onSubmit to each of the forms, but no action.

Your onSubmit will call a function that will change the background colour of the square.

 

You will need to google:

How to make an image a submit button for a form.

How to use the onSubmit functionality of a form.

How to change the colour of a div using javascript (assuming the squares are divs, either way, you need to research how to change something using javascript).

 

That should just about get you what you want..

 

Denno

Link to comment
Share on other sites

You don't need a form at all to do this.  That would be overkill, not to mention that forms are used to submit data, not handle UI changes.

 

All you need to do is attach an onclick event to each square.  Inside that click event's handler function, have it grab a hold of the current page background color and change it to whatever you want.  It's a couple of lines of code, depending on exactly how you have it set up.

 

The particular details rely on how exactly you want this to be structured in your HTML.  So, if you can supply some sample code, I can point you in the right direction.

Link to comment
Share on other sites

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.