Color remains the same after onClick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nma
    New Member
    • Aug 2007
    • 93

    Color remains the same after onClick

    Hi

    Can anyone help me..
    How can I make onClick color remains the same =red?
    Currently if it is mouseover it change to red, onmouseover it change to original background color. I need it to remain red once it is click so that I know where is the last location I click on the bar


    [PHP]

    <td width="<?php echo ($cellwidth); ?>" height="17" bgcolor="yellow " onMouseOver="th is.style.backgr ound ='red'"
    onMouseOut="thi s.style.backgro und='yellow'" onClick="window .frames.eventsD isplay.location = 'MB2eventlistTi meline.php?movi eID=<?php echo $movie->movieID;?>&eve nts=<?php echo $timeline->eventCategory; ?>#<?php echo $timeline->eventID?>';par ent.notesForm.e ventID.value=<? php echo $timeline->eventID?>;pare nt.resetNotes() ;" style="cursor: pointer;"></td>

    [/PHP]

    Stuck..

    nma
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You can set a variable onclick which you can test onmouseover.

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      You could use a custom attribute to know which are selected..


      Toggle color on/off with mouseover and mouseout events included.. Example provided
      Code:
      <table>
      <tr>
      	<td 
      	  onmouseover="if (this.getAttribute('selected')==null) { this.setAttribute('selected', ''); } if (this.getAttribute('selected')=='') { this.style.color = '#ff0000'; }"
      	  onmouseout="if (this.getAttribute('selected')=='') { this.style.color = ''; }"
      	  onclick="if (this.getAttribute('selected')!='') { this.setAttribute('selected', ''); this.style.color=''; } else { this.setAttribute('selected', 'true'); this.style.color='#ff0000'; }"
      	  style="cursor: pointer;">
      	Blah
      	</td>
      </tr>
      </table>
      I made this sample with font color rather than background color... you can change it.

      Comment

      • nma
        New Member
        • Aug 2007
        • 93

        #4
        Originally posted by iam_clint
        You could use a custom attribute to know which are selected..


        Toggle color on/off with mouseover and mouseout events included.. Example provided
        Code:
        <table>
        <tr>
        	<td 
        	  onmouseover="if (this.getAttribute('selected')==null) { this.setAttribute('selected', ''); } if (this.getAttribute('selected')=='') { this.style.color = '#ff0000'; }"
        	  onmouseout="if (this.getAttribute('selected')=='') { this.style.color = ''; }"
        	  onclick="if (this.getAttribute('selected')!='') { this.setAttribute('selected', ''); this.style.color=''; } else { this.setAttribute('selected', 'true'); this.style.color='#ff0000'; }"
        	  style="cursor: pointer;">
        	Blah
        	</td>
        </tr>
        </table>
        I made this sample with font color rather than background color... you can change it.
        Hi iam_clint
        I'm correcting the codes like below..there is red color on the cell once I mouseout but it remains many red color appear depending on as many times I click on the cell.
        What is wrong with the code?
        Thanks

        [PHP]
        <td width="<?php echo ($cellwidth); ?>" height="17" bgcolor="#00ff0 0" onmouseover="if (this.getAttrib ute('selected') ==null) { this.setAttribu te('selected', ''); } if (this.getAttrib ute('selected') =='') { this.style.back ground = '#ff0000'; }" onmouseout="if (this.getAttrib ute('selected') =='') { this.style.back ground = ''; }" onclick="if (this.getAttrib ute('selected') !='') { this.setAttribu te('selected', ''); this.style.back ground=''; } else { this.setAttribu te('selected', 'true'); this.style.back ground='#ff0000 '; } "window.frames. eventsDisplay.l ocation = 'MB2eventlistTi meline.php?movi eID=<?php echo $movie->movieID;?>&eve nts=<?php echo $timeline->eventCategory; ?>#<?php echo $timeline->eventID?>'; parent.notesFor m.eventID.value =<?php echo $timeline->eventID?>;pare nt.resetNotes() ; " style="cursor: pointer;"></td>
        [/PHP]

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          I don't fully understand your english... are you trying to make it so only 1 cell can be selected at a time?

          Comment

          • nma
            New Member
            • Aug 2007
            • 93

            #6
            Originally posted by iam_clint
            I don't fully understand your english... are you trying to make it so only 1 cell can be selected at a time?
            Sorry for my poor explaination..
            Yes I want it only at one time cell selected (say red color)..then when user click at another cell that current cell will be selected and the previous cell back to original color

            It is actually a timeline bar...

            p/s the code you give is correct in terms of highlighted number of click user made on the timeline show red colored cell appear along the timeline/cells

            Please help..

            Comment

            • iam_clint
              Recognized Expert Top Contributor
              • Jul 2006
              • 1207

              #7
              so you need to make a function for onclick.. in this function you iterate through all cells..

              Example
              Code:
              function resetHighlight() {
              	var ex=document.getElementById("tablebody").getElementsByTagName("TD"); 
                  for (var i=0; i<ex.length; i++) {
              		if (ex[i].getAttribute("selected")!="") {
              			ex[i].style.background="";
              			ex[i].setAttribute("selected", "");
              		}
              	}
              }
              
              function highlight(obj) {
                  resetHighlight();
              	obj.setAttribute("selected", "true");
              	obj.style.background="#FF0000";
              }

              Comment

              • nma
                New Member
                • Aug 2007
                • 93

                #8
                Originally posted by iam_clint
                so you need to make a function for onclick.. in this function you iterate through all cells..

                Example
                Code:
                function resetHighlight() {
                	var ex=document.getElementById("tablebody").getElementsByTagName("TD"); 
                    for (var i=0; i<ex.length; i++) {
                		if (ex[i].getAttribute("selected")!="") {
                			ex[i].style.background="";
                			ex[i].setAttribute("selected", "");
                		}
                	}
                }
                
                function highlight(obj) {
                    resetHighlight();
                	obj.setAttribute("selected", "true");
                	obj.style.background="#FF0000";
                }
                Hi iam_clint,
                But how am I going to insert in my original coding..does this function need to be declare somewhere in the beginning of my codes.
                At the moment in my original codes when onClick happen it will get some event (video keyframe list). Can I combine this two event (the code you give above + the one in my original codes) within one single onClick lines of codes?


                Code:
                ........onClick="window.frames.eventsDisplay.location = 'MB2eventlistTimeline.php?movieID=<?php echo $movie->movieID;?>&events=<?php echo $timeline->eventCategory;?>#<?php echo $timeline->eventID?>';parent.notesForm.eventID.value=<?php echo $timeline->eventID?>;parent.resetNotes();"...........
                Last edited by acoder; Sep 18 '08, 01:05 PM. Reason: fixed code tags

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Yes, it's relatively easy to combine. Move all the onclick code into its own function. I see that you're using PHP code within the onclick, so make the function generic and pass the PHP values as arguments to the function.

                  Comment

                  Working...