Solved

Add border to link onclick

Posted on 2012-03-31
4
279 Views
Last Modified: 2012-06-21
I have made a simple drawing canvas with the option to change colour of the pen which works fine (see code below).

I would like to either add a border or highlight the colour that is active.

Could someone give me some help with what I need to add to the javascript to do this.

<a class="colorPicker" href="#" onclick="setColor('255,255,255');return false;"><img src="images/colour_white.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" onclick="setColor('000,000,000');return false;"><img src="images/colour_black.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" onclick="setColor('255,000,000');return false;"><img src="images/colour_red.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" onclick="setColor('000,192,000');return false;"><img src="images/colour_green.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" onclick="setColor('000,000,255');return false;"><img src="images/colour_blue.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" onclick="setColor('255,255,000');return false;"><img src="images/colour_yellow.gif" width="40" height="50"></a>

Open in new window

function setColor(col)
{
	r = (col.substring(0,3));
	g = (col.substring(4,7));
	b = (col.substring(8,11));

//link that was pressed add border around picture
//remove other borders.

}

Open in new window

0
Comment
Question by:Steve Tinsley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Tony van Schaik
ID: 37792080
You mean something like this? I used jQuery to do the trick:

<!DOCTYPE html>
<html>
    
    <head>
        <style>
            p {
                margin: 10px;
                font-size:16px;
            }
            .selected {
                color:blue;
                border:1px solid #ff0000;
            }
        </style>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    
    <body>
        <div class="colorpicker">
            <a href="/"><img src="http://farm7.staticflickr.com/6156/6178772100_f64a36b5b6.jpg" width="500" height="379"></a>
            <a href="/"><img src="http://farm8.staticflickr.com/7025/6783068129_71617748ce.jpg" width="500" height="375"></a>
        </div>
        <script>
            $(function () {
                $(".colorpicker img").click(function (e) {
                    e.preventDefault();
                    $(".colorpicker img").addClass("selected").not(this).removeClass("selected");
                });
            });
        </script>
    </body>

</html>

Open in new window


Or did you mean different border colors with onclick?
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 37793086
Try something like this jsfiddle.

$(".colorPicker").click(function() {
    $(".colorPicker").not(this).removeAttr("style");
    $(this).attr("style", $(this).attr("for"));
});

Open in new window

0
 

Author Comment

by:Steve Tinsley
ID: 37793273
Hi Guys,
Thanks for the ideas.
Proculopsis - I have had a play with your idea to make it work for me but am struggling...

HTML
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('255,255,255');return false;"><img src="images/colour_white.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('000,000,000');return false;"><img src="images/colour_black.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('255,000,000');return false;"><img src="images/colour_red.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('000,192,000');return false;"><img src="images/colour_green.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('000,000,255');return false;"><img src="images/colour_blue.gif" width="40" height="50"></a>
<a class="colorPicker" href="#" for="border: solid 3px black;" onclick="setColor('255,255,000');return false;"><img src="images/colour_yellow.gif" width="40" height="50"></a>

Open in new window


JS
function setColor(col)
{
	r = (col.substring(0,3));  // Set drawing color
	g = (col.substring(4,7));  //Set drawing color
	b = (col.substring(8,11));  //Set drawing color
		
    $(".colorPicker").not(this).removeAttr("style");
    $(this).attr("style", $(this).attr("for"));

}

Open in new window


In the above code the border doesnt show when I click link.
I can see how it works looking at the jsfiddle you did.

Any more pointers
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37794696
Variable this is not meaningful in colorPicker but it is a courtesy of a jQuery event handler.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question