Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Again, unrelated to OP problem, but with this minor addition I would be able to replicate the color chosen...
    Code:
      function pickThis( ) {
        document.getElementById("theChoice").style.backgroundColor = this.style.backgroundColor;
        document.getElementById('theChoice').innerHTML = this.style.backgroundColor;
        for ( var d = 0; d < divs.length; ++d ) { divs[d].style.borderColor = "black"; }
        this.style.borderColor = "red";
      }

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    This line:
    Code:
    document.getElementById('theChoice').innerHTML = this.style.backgroundColor;
    doesn't work as you might expect in all browsers. I found instances where the color thus returned is something like rgb(37,28,101) or where I used #333 and got back #333333. Might have been only some older browsers. I haven't checked recently.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    While I have not checked all browsers, I am using FF v19.0.2 to get this display.
    I did not know it gave different values for different browsers. (Not having checked )

    I do understand the rgb(r,g,b) is just an internal (decimal) representation of the color.
    I assumed it could be translated to a hexidecimal representation with a bit more logic.

    Code:
    <script type="text/javascript">
    var c = 'rgb(16,32,64)';
    var tarr = c.split(/\(|\)/);
    var rgb = tarr[1].split(',');
    var str = '';  for (var i=0; i<rgb.length; i++) { str += Number(rgb[i]).toString(16); }
    alert('#'+str);
    </script>

  4. #19
    New Coder
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    What have you tried? And why do you need server-side code simply to count the number of black and white cells when you could do that all in JavaScript just as simply, if not much more so? Is this a homework assignment, hence the (somewhat odd) requirements?
    I have tried something with jquery to load ajax, but it doesnt really work. Is this even good? I have tried to put the color values into a form control and submit that form to php script, so that i can send it to ajax.php. And its not a homework assignment, its a task from a book, that im trying to solve, while learning something new.

    Code:
    $.("#hren").load("ajax.php"+$.("#zaseka").serialize());

    and ajax.php:
    Code:
    $black= 0;
    $white= 0;
    foreach($_REQUEST as $key => $value)
    {
    if($value == "<black color>")
    $black++;
    else if($value == "<white color>")
    $white++;
    
    }
    
    echo "In the table there are $black black cells and $white white cells.";
    Last edited by bigbada; 03-09-2013 at 08:32 AM.

  5. #20
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    This line:
    Code:
    document.getElementById('theChoice').innerHTML = this.style.backgroundColor;
    doesn't work as you might expect in all browsers. I found instances where the color thus returned is something like rgb(37,28,101) or where I used #333 and got back #333333.
    There are still at least two different formats. I just did a quick test with five different browsers retrieving the value from an area with background-color set to #666 - the latest versions of Chrome, Opera, Safari and Firefox all returned rgb(102,102,102) while IE8 returned #666. Changing the value to #666666 had no effect on any of the browsers except IE8 which is obviously returning the value using the format it was set using instead of the standard one.

    Setting the colour using rgba returned the same value as was set (but with the transparency value slightly different) except in IE that didn't return anything for that value.

    I don't know if IE 9 and 10 behave like IE8 or like modern browsers as I don't have a copy of them available at the moment to test with. From memory IE6 and 7 are the same as IE8.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •