Fun Stuff > CLIKC

Need help (lots) with Javascript

<< < (11/13) > >>

bryntheskits:

--- Quote from: Sorflakne on 27 Aug 2013, 11:30 ---Ah ok.  Gonna keep the ;'s then, since I'm still getting used to the code.

Ok, here's another thing I've run into, and it took some hair pulling and whatnot to figure out.  Here's code for two buttons:


--- Code: ---<button onclick="timedCount()">Start count</button>

<input type="button" value="Start count" onClick="timedCount()" />

--- End code ---
Both point to the same function when placed in the same block of code, but for some reason, only the top one works (this is for a basic timer; text of the function is the same for both attempts).  The second one creates a button, but its nonfunctioning.  I've had this happen several other times, and each time when I went back and changed the code from the second format to the first's, then it works fine.  What causes one to work and the other to not?

--- End quote ---

Get rid of the / after the onclick code, don't need that.

Other than that I cannot see what the problem could be, are you using anything to debug the Javascript? Even Chrome comes with a developer console, very handy.

EDIT: Nevermind, found the problem (what I could replicate here, anyway); onclick is supposed to be all lowercase, since methods are case sensitive it will only work that way, the 2nd bit of code you have there is in camel case (onClick) when it should be lower, like the 1st bit of code (onclick).

snalin:
So, I wrote some test code:


--- Code: ---<html>
<button onclick="timedCount(this)">Start count</button>
<input type="button" value="Start count" onClick="timedCount(this)" />
<script>
function timedCount(btn) {
console.log("pressed from: " + btn);
}
</script>
</html>

--- End code ---

The "this" inside the onclicks just sends the button element in, so the timedCount method can print it out.
Pressing the first button results in: "pressed from: [object HTMLButtonElement] "
Pressing the second button results in: "pressed from: [object HTMLInputElement] "

So it seems like both input and button works from my side. Above poster is correct in that the onclick should be all lower case, but chrome handles both onclick and onClick in both the button and the input. Not sure about other browsers. You are wrong on the / not being required, it closes off the input tag... but it doesn't seem to matter if it's there or not. Javascript :psyduck:

Sorflakne, can you post your code? It'd help to be able to look at it.

pwhodges:

--- Quote from: bryntheskits on 27 Aug 2013, 11:49 ---Get rid of the / after the onclick code, don't need that.
--- End quote ---

--- Quote from: snalin on 27 Aug 2013, 12:37 ---You are wrong on the / not being required, it closes off the input tag... but it doesn't seem to matter if it's there or not.
--- End quote ---

<input> has no end tag in HTML (any version); however, in XHTML, the end tag is required.

snalin:
Erp, didn't spot that, you are right.

bryntheskits:

--- Quote from: snalin on 27 Aug 2013, 12:37 ---but chrome handles both onclick and onClick in both the button and the input.
--- End quote ---
Strange, for lack of proper time to test I used the developer console to change onclick to onClick and the onClick and it's variable disappears.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version