Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: on onclick: change row class

  1. #1
    Join Date
    Dec 2005
    Posts
    133
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default on onclick: change row class

    when click on row then show a different class(bg and font color changes)

    Any suggestions?
    txs

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Any suggestions?
    Yes, GOOGLE!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In about its simplest form:

    HTML Code:
    <span class="bob" onclick="this.className=this.className=='bob'? 'mary' :
    'bob';" title="Click for Class Change">Bob or Mary on click</span>
    Note: You would define the .bob and .mary classes in your stylesheet.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    emanuelle (05-01-2008)

  5. #4
    Join Date
    Dec 2005
    Posts
    133
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default

    <tr class="bob" onclick="this.className=this.className=='bob'? 'mary' :
    'bob';"><td>1</td></tr>

    <tr class="bob" onclick="this.className=this.className=='bob'? 'mary' :
    'bob';"><td>2</td></tr>

    <tr class="bob" onclick="this.className=this.className=='bob'? 'mary' :
    'bob';"><td>3</td></tr>

    If I click on 1 it will hightlights if I click on 2 it will highlight but then 1 goes back to default color.
    Only one highlighted at the time

    How do I adjust this?

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I am assuming that you mean you want it to work that way, as it shouldn't be doing it like that as written in your post. For that, you need some way to collect all of the elements and act on them as a group. Also you have to decide what happens when only one is highlighted and that one is clicked on again. In most cases, you want them all to then be not highlighted.

    Give this a shot:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .bob {
    color:red;
    }
    .mary {
    background-color:pink;
    }
    </style>
    <script type="text/javascript">
    function togGroup(el, classes){
    for (var els=document.getElementsByTagName(el.tagName), i = els.length-1; i > -1; --i)
    if (els[i]!==el&&els[i].className==classes.b)
    els[i].className=classes.a;
    el.className=el.className==classes.a? classes.b : classes.a;
    }
    </script>
    </head>
    <body>
    <table>
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>1</td></tr>
    
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>2</td></tr>
    
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>3</td></tr>
    </table>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    emanuelle (05-01-2008)

  8. #6
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi.
    How can I apply the a:hover css attribute to the bob and or mary classes above? Sorry, I really tried but no luck. THANK YOU FOR YOUR HELP!!!

  9. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You cannot. They aren't a tags. However, you can apply the :hover pseudo class to anything (won't work in IE 6, but works in all others), ex:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .bob {
    color:red;
    }
    .mary {
    background-color:pink;
    }
    .bob:hover {
    background-color: pink;
    color: black;
    }
    </style>
    <script type="text/javascript">
    function togGroup(el, classes){
    for (var els=document.getElementsByTagName(el.tagName), i = els.length-1; i > -1; --i)
    if (els[i]!==el&&els[i].className==classes.b)
    els[i].className=classes.a;
    el.className=el.className==classes.a? classes.b : classes.a;
    }
    </script>
    </head>
    <body>
    <table>
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>1</td></tr>
    
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>2</td></tr>
    
    <tr class="bob" onclick="togGroup(this, {a:'bob', b:'mary'});"><td>3</td></tr>
    </table>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #8
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks jscheuer1 for the script.

    but I realized that if I apply the script to several <TR> when I click a row moves which I clicked.

    how I can do to make the rows already selected remain with that class until you click again?

  11. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If I understand the question, get rid of the highlighted:

    Code:
    <script type="text/javascript">
    function togGroup(el, classes){
    for (var els=document.getElementsByTagName(el.tagName), i = els.length-1; i > -1; --i)
    if (els[i]!==el&&els[i].className==classes.b)
    els[i].className=classes.a;
    el.className=el.className==classes.a? classes.b : classes.a;
    }
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. #10
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    jscheuer1,

    I want the rows that I select remain selected until you click the row to deselect.

Bookmarks

Posting Permissions

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