473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Changing border colour on click?

I want to change the colour of a border around an image when a user clicks on the image.

Not sure how to go about this, i have tried a few things but it doens't seem to work.

here is my image.

<img src="images/canvas_colours/images/canvas_taupe_small.gif" alt="Taupe" name="taupe_small" id="taupe_small" style="cursor:hand;border-Style:solid;border-Width:2px;border-Color:white">

This is what I tried.

<img src="images/canvas_colours/images/canvas_taupe_small.gif" alt="Taupe" name="taupe_small" id="taupe_small" style="cursor:hand;border-Style:solid;border-Width:2px;border-Color:white;" onclick="border-Color:red;">

Any help would be great.

Thanks.
Jun 7 '07 #1
23 8702
Logician
210 100+
This is what I tried.

<img src="images/canvas_colours/images/canvas_taupe_small.gif" alt="Taupe" name="taupe_small" id="taupe_small" style="cursor:hand;border-Style:solid;border-Width:2px;border-Color:white;" onclick="border-Color:red;">
Try:
Expand|Select|Wrap|Line Numbers
  1. onclick="this.style.borderColor='red'"
What does clicking the image do? If there's any other script associated with clicking the image, there may be a problem.
Jun 8 '07 #2
That works, problem I have now is that I have 4 clickable images that now change the border colour on click.

Probem being that when I lets say click on image 1 the border colour changes and if i click on image 2 the border colour changes as well.

I only want one image to be highlighted with the border colour, so when if image 1 has the border colour highlighted and i click image 2 i want image 2 to have the border colour and image one to go back to white.

<img src="images/canvas_colours/images/canvas_taupe_small.gif" alt="Taupe" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onClick="this.style.borderColor='#336699'; "/>
<img src="images/canvas_colours/images/canvas_pink_small.gif" onClick="this.style.borderColor='#336699'; " style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" alt="Pink" name="pink_small" id="pink_small" />
Jun 8 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

you may use the following method:

[HTML]
<script>
function toggle_highlight(id, color) {
var images = document.getElementsByTagName('image');

for (var i = 0; i < images.length; i++) {
var image = images[i];

image.style['border-color'] = image.id == id ? color : '';
}
}
</script>


<img src="images/canvas_colours/images/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onclick="toggle_highlight(this.id, #336699');"/>
<img src="images/canvas_colours/images/canvas_pink_small.gif" onclick="toggle_highlight(this.id, '#336699');" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
[/HTML]

solves the problem ;)

kind regards ...
Jun 8 '07 #4
gits
5,390 Expert Mod 4TB
and ... of course you may improve it the folowing way:

[HTML]
<script>
var highlight_color = '#336699';

function toggle_highlight(id) {
var images = document.getElementsByTagName('image');

for (var i = 0; i < images.length; i++) {
var image = images[i];

image.style['border-color'] = image.id == id ? highlight_color : '';
}
}
</script>


<img src="images/canvas_colours/images/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onClick="toggle_highlight(this.id);"/>

<img src="images/canvas_colours/images/canvas_pink_small.gif" onClick="toggle_highlight(this.id);" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
[/HTML]

declaring the highlight-color avoids to pass it to the function whereever you have to call it ... and you've got only ONE point to change it, in case you have to!

kind regards ...
Jun 8 '07 #5
ronnil
134 Expert 100+
does all of your four images have the same id? (taupe_small) if so that might be the case why all of your images change color at once.
Jun 8 '07 #6
gits
5,390 Expert Mod 4TB
does all of your four images have the same id? (taupe_small) if so that might be the case why all of your images change color at once.
no ... that seems to be ok ... the ids of his example are distinct. with the first solution the border of every image changes onclick and he wants to highlight the clicked one only ...

but of course: ids always have to be distinct ... and it is a good point to mention it ... sometimes there are mistakes with that ... especially when creating elements dynamically and setting ids ... but with getElementById this is easy to debug ...

kind regards ...
Jun 8 '07 #7
Thanks for the reply.

I get javascript errors with that.

I put that in a single page to test and it says line 32 object expected.

<script>

function toggle_higlight(id, color) {

var images = document.getElementsByTagName('image');
for (var i = 0; i < images.length; i++) {

var image = images[i];

image.style['border-Color'] = image.id == id ? color : '';

}

}
#
</script>

<body>



<img src="images/canvas_colours/images/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onclick="toggle_highlight(this.id, '#336699');"/>

<img src="images/canvas_colours/images/canvas_pink_small.gif" onclick="toggle_highlight(this.id, '#336699');" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
Jun 8 '07 #8
gits
5,390 Expert Mod 4TB
there were typos ... i've corrected them ... try again ... (have a look at the function-name and the function-call) - do you see it? :) ...
Jun 8 '07 #9
yes noticed it now (spelling mistake).

Problem is now, it doesn't change the border colour ?????
Jun 8 '07 #10
this is what i have put in a page to test

<script language="javascript">

function toggle_highlight(id, color) {

var images = document.getElementsByTagName('image');
for (var i = 0; i < images.length; i++) {

var image = images[i];

image.style['border-Color'] = image.id == id ? color : '';

}

}

</script>

<body>



<img src="imagescanvas/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onclick="toggle_highlight(this.id, '#336699');"/>

<img src="imagescanvas/canvas_pink_small.gif" name="pink_small" id="pink_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onclick="toggle_highlight(this.id, '#336699');" />
Jun 8 '07 #11
gits
5,390 Expert Mod 4TB
yes noticed it now (spelling mistake).

Problem is now, it doesn't change the border colour ?????
did you mix the 2 solutions? they work in distinct ways ...

kind regards ...
Jun 8 '07 #12
ah, noticed you have changed the script.

i will test it
Jun 8 '07 #13
this is what i have now (doesn't change the border colour)

<script language="javascript">

var highlight_color = '#336699';

function toggle_highlight(id) {

var images = document.getElementsByTagName('image');

for (var i = 0; i < images.length; i++) {

var image = images[i];
image.style['border-Color'] = image.id == id ? highlight_color : '';

}

}

</script>







<body>



<img src="imagescanvas/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onClick="toggle_highlight(this.id);"/>

<img src="imagescanvas/canvas_pink_small.gif" onClick="toggle_highlight(this.id);" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
Jun 8 '07 #14
gits
5,390 Expert Mod 4TB
ah, noticed you have changed the script.

i will test it

ok ... more typos ... replace: var images = document.getElementsByTagName('image'); with var images = document.getElementsByTagName('img'); of course ...
Jun 8 '07 #15
many thanks for your help . . . but still no border colour change ??

<script language="javascript">

var highlight_color = '#336699';

function toggle_highlight(id) {

var images = document.getElementsByTagName('img');

for (var i = 0; i < images.length; i++) {

var image = images[i];
image.style['border-Color'] = image.id == id ? highlight_color : '';

}

}

</script>







<body>



<img src="imagescanvas/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onClick="toggle_highlight(this.id);"/>

<img src="imagescanvas/canvas_pink_small.gif" onClick="toggle_highlight(this.id);" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
Jun 8 '07 #16
gits
5,390 Expert Mod 4TB
... and change the line

image.style['border-Color'] = image.id == id ? highlight_color : '';

to:

image.style.borderColor = image.id == id ? color : 'white';

then it works ... sorry for not testing it ... i'm at the office right now ... and have little time ...

kind regards ...
Jun 8 '07 #17
I appreciate your help.

but still not working, i wish i could fix this myself but i'm pretty novice at this.

I get 'color' is undefined.

<script language="javascript">

var highlight_color = '#336699';

function toggle_highlight(id) {

var images = document.getElementsByTagName('img');

for (var i = 0; i < images.length; i++) {

var image = images[i];
image.style.borderColor = image.id == id ? color : 'white';


}

}

</script>
Jun 8 '07 #18
gits
5,390 Expert Mod 4TB
... yes ... aaaaargh ... whats wrong today ;) use highlight_color instead of color in the last changed line ... color = undefined gives you that hint

kind regards
Jun 8 '07 #19
gits
5,390 Expert Mod 4TB
explaination:

... have a close look at the function: it only receives an id ... the one of the image that should colorchange ... we pass it from onclick with this.id ...

the first solution received the color as well ... and set it to the image-style ... the second and the actual one uses a global variable highlight_color instead of color ... and the function doesn't know about a variable color any longer ... so javascript tells you that color is undefined ...
Jun 8 '07 #20
sorry, but i'm completely lost.
Jun 8 '07 #21
gits
5,390 Expert Mod 4TB
sorry, but i'm completely lost.
the line:

image.style.borderColor = image.id == id ? color : 'white';

has to be:

image.style.borderColor = image.id == id ? highlight_color : 'white';
Jun 8 '07 #22
Thank you very much for you time and patience :-)
Jun 8 '07 #23
gits
5,390 Expert Mod 4TB
... at last we have it ;) ... and now here is the working version:

[HTML]
<script>
var highlight_color = '#336699';

function toggle_highlight(id) {
var images = document.getElementsByTagName('img');

for (var i = 0; i < images.length; i++) {
var image = images[i];

image.style.borderColor = image.id == id ? highlight_color : 'white';
}
}
</script>

<img src="images/canvas_colours/images/canvas_taupe_small.gif" name="taupe_small" id="taupe_small" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" onClick="toggle_highlight(this.id);"/>

<img src="images/canvas_colours/images/canvas_pink_small.gif" onClick="toggle_highlight(this.id);" style="cursor:hand; border-Style:solid; border-Width:2px; border-Color:white;" name="pink_small" id="pink_small"/>
[/HTML]
Jun 8 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dj Frenzy | last post by:
Hi, I know how to use javascript to change a background image to another background image, and how to change a background colour to another background colour. Is there a way to change an image to a...
1
by: xtrmntr: a northern soul | last post by:
alright there, i'm pretty much throwing myself on everyone here's mercy, as i'm struggling to even articulate what i mean, never mind create the behaviour i want. i've a website with a...
34
by: The Good Son | last post by:
I'm trying to get my head around a problem that has me baffled. The following element: #wrapper {border:1px solid #000;background:#fff;} Renders as expected when using this DTD: ...
7
by: redneon | last post by:
Is it possible to change the border colour of a button whos FlatStyle is set to Flat? It's be nice to be able to change the width of the border too. Any ideas? Darrell
6
by: Dan Soendergaard | last post by:
Hi, I'm writing my own control and I would like it to integrate nicely into the rest of my application, which uses the standard controls primarely. I therefore wanted my control to use the same...
8
by: Steve | last post by:
Hi All Is there a way to change the colour of a Groupbox border in VB.net 2005 I want to change it to white Can it be done in the onpaint event? Regards Steve
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
4
by: sgxbytes | last post by:
Hi, My html has <Table style="year-button-archive" width="60%" > <tr> <td class="gold" > <a title="year">By Year:</a> <td class="gold"> <a id = "2008"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.