Javascript to determine stroke color and change it depending on that color

Discussion in 'JavaScript' started by streulich7, Jun 12, 2007.

  1. #1
    I have an ellipse that starts out with a stroke color of gray. I want to write some javascript that will basically work like this: if the stroke is gray, click the ellipse to make the stroke green, if the stroke is green(maybe because I clicked it previously to make it green), then the stroke will turn gray again. I want to be able to click it as many times as I want and have the stroke alternate between green and gray every time I click the ellipse. Thanks for the help!
     
    streulich7, Jun 12, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    post the code you have so far .....
     
    krakjoe, Jun 12, 2007 IP
  3. streulich7

    streulich7 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sure...

    right now I can only click it the first time and change it from gray to green, though.

    Here's the ellipse I'm using:

    <Ellipse x:Name="powerButton" MouseLeftButtonDown="power_on" Fill="DarkGray" Stroke="Gray" StrokeThickness="4" Width="48" Height="48" Canvas.Left="56" Canvas.Top="56"/>

    Here's the Javascript:

    function power_on(sender, args) {
    sender.findName("powerButton").Stroke = "lime";
    }

    very simple. I was playing around using a power_off function and inserting a different mouse event in the ellipse code, but that's not ideal. I want to use the mouseleftbuttondown for both, on the same object. maybe something with an if else statement?
     
    streulich7, Jun 12, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Sorry, I have no clue what language that is, I have never seen that tag before.

    Can I see a page with that on ??
     
    krakjoe, Jun 12, 2007 IP
  5. streulich7

    streulich7 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's XAML. I'm working with Silverlight. Honestly that's not the part that matters. The only thing you need to know from the XAML part is that MouseLeftButtonDown calls the function power_on. I only need help with the javascript part.

    If you want to learn more about Silverlight and XAML go to http://silverlight.net/Learn/ and do the quickstarts.
     
    streulich7, Jun 12, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    I don't think you can use findName is xaml ....

    You'd probably have better luck asking in a .NET forum.....
     
    krakjoe, Jun 12, 2007 IP
  7. streulich7

    streulich7 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    findName is working right now. That code I put in my post works. I only need help with syntax for an if else statement, I'll work out how the rest. I can't ask about javascript in an asp.net forum. nothing i'm doing here deals with .net. Maybe show me how you would do it with your own code, maybe substitute html for the xaml or something. I just need to find an example somewhere of how you would click on an object to get different colors, regardless of whether you use xaml or html or whatever else. How would you do it?
     
    streulich7, Jun 12, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    
    <script language="javascript">
    	function swap_color( obj )
    	{
    		return obj.style.backgroundColor == 'blue' ? obj.style.backgroundColor = 'red' : obj.style.backgroundColor = 'blue' ;
    	}
    	function swap_color_else( obj )
    	{
    		var color = obj.style.backgroundColor ;
    		if( color == 'red' )
    		{
    			return ( obj.style.backgroundColor = 'blue' );
    		}	
    		else
    		{
    			return ( obj.style.backgroundColor = 'red' );
    		}
    	}
    </script>
    <div onclick="swap_color( this );">
    the first example
    </div>
    <div onclick="swap_color_else( this );">
    the second example
    </div>
    
    PHP:
    Id do it like that, hope it helps .......
     
    krakjoe, Jun 12, 2007 IP
  9. streulich7

    streulich7 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you!! I'll try it out and let you know what happens.
     
    streulich7, Jun 12, 2007 IP