d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Function Question
12Next
Add Reply New Topic New Poll
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 8 2012 05:40pm
hello guys, so i am trying to program a function that when the user clicks a cell in a table it changes the background color of the cell.
so far i got this, but i dont understand why it is not working:

Code
<!DOCTYPE html>
<HTML>

<HEAD>

 <TITLE>Change BG color on click</TITLE>
 
 <SCRIPT>
  function  bgBlue()
  {
   this.bgcolor='#3700FF'
  }
 </SCRIPT>
 
</HEAD>

<BODY>
 
 <H1 align="center">Click to change background color</H1>
 
 <TABLE width="100%" border="1px" cellspacing="0px" bgcolor="#FF0000">
 
  <TR>
   <TD onclick="bgBlue()">
    <BR />
   </TD>
  </TR>
 
 </TABLE>
 
</BODY>

</HTML>


as you can see i just want to perform a basic bg color change but i dont want to do this because i will have multiple hundreds cell and i want them to share the same function:
Code
onclick="this.bgcolor='#3700FF'"


thanks for help

This post was edited by francisvolley on Sep 8 2012 05:44pm
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 8 2012 06:46pm
can anybody help? thanks
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 8 2012 06:57pm
It's been an hour. quit being impatient. I'm sure saturday evening is prime time for Programmer's Haven

This post was edited by carteblanche on Sep 8 2012 06:57pm
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 8 2012 08:19pm
Quote (carteblanche @ 8 Sep 2012 19:57)
It's been an hour. quit being impatient. I'm sure saturday evening is prime time for Programmer's Haven


i have some views i thought people dont care
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Sep 11 2012 10:14pm
Quote (francisvolley @ 9 Sep 2012 04:19)
i have some views i thought people dont care


If people don't care, do you really think they'll care if you write "can anybody help? thanks"?
For me, it would go from "Maybe I'll help him later" to "What an impatient kid, he can try harder to get some help".

Your problem is that you didn't google the issue first. I just did and found out the issue with the first result.

This in a function will also refer to the function itself, not the element which calls the function. So, we call the function with a reference to the element itself (this).
http://jsfiddle.net/CxDXj/

This post was edited by eagl3s1ght on Sep 11 2012 10:17pm
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 12 2012 11:32am
Quote (eagl3s1ght @ 11 Sep 2012 23:14)
If people don't care, do you really think they'll care if you write "can anybody help? thanks"?
For me, it would go from "Maybe I'll help him later" to "What an impatient kid, he can try harder to get some help".

Your problem is that you didn't google the issue first. I just did and found out the issue with the first result.

This in a function will also refer to the function itself, not the element which calls the function. So, we call the function with a reference to the element itself (this).
http://jsfiddle.net/CxDXj/


thanks

also what i wanted to do after is to change the color depending of what key the user presses:
ex:

if he has his mouse on cell 1 and press b -> color = blue
if he has his mouse on cell 1 and press r -> color = red
if he has his mouse on cell 2 and press b -> color = blue

and so on

how would i program this? i would need an if statement in my function? or create 2 functions depending on what he inputs?

thanks
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Sep 12 2012 11:58am
you'll only need one method. the input will vary, so you'll just to need to pass the input by parameter, and treat that input with if's inside your function. You'll need to work with OnMouseOver and OnKeyPress events to make it work the way you want.
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 12 2012 12:09pm
Quote (StormHasHe @ 12 Sep 2012 12:58)
you'll only need one method. the input will vary, so you'll just to need to pass the input by parameter, and treat that input with if's inside your function. You'll need to work with OnMouseOver and OnKeyPress events to make it work the way you want.


ok but how will i access the key press?

like

if ( onkeypress == b )
code


??

This post was edited by francisvolley on Sep 12 2012 12:09pm
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Sep 12 2012 01:37pm
just google it.

http://www.devguru.com/technologies/ecmascript/quickref/evhan_onkeypress.html

edit:

alternatively, you could use jquery to do it.

Code
$("selector").bind('keydown', function (event) {
           if (event.which == "your key code here") {
               //do stuff here
           }
       });


this was a generic example. For real, you could have something like this:
Code
$("#guaranteesFilterContent").bind('keydown', function (event) {
           if (event.which == 13) {
               fnSearch(true);
               return false;
           }
       });


to know the codes that goes with each key, just check this website: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
or alternatively, google for "javascript keypress codes"

edit2:

of course, your onmouseover code will call those functions, who will be listening for the correct keypress only when inside a cell.

This post was edited by StormHasHe on Sep 12 2012 01:54pm
Member
Posts: 18,732
Joined: Sep 3 2008
Gold: 0.00
Sep 12 2012 02:27pm
ok ty i will try this tomorow i have work tonight
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll