javascript - function not being called, do I need a window.load? -
i trying change css stylings of item when it's clicked.
ryan.js
function ryanclicked(id){ document.getelementbyid(id).style.color = "blue"; alert("asdsa"); }
product.html
<head> <script src="ryan.js"></script> </head> <body> <div id ="sizebutton1"class="sizebutton" onclick="ryanclicked(sizebutton1)"> s </div> <div id ="sizebutton2"class="sizebutton" onclick="ryanclicked(sizebutton2)"> m </div> <div id ="sizebutton3"class="sizebutton" onclick="ryanclicked(sizebutton3)"> l </div>
i assume need onready or onload somewhere? haven't done js in while. page has jquery included can use jquery ready event. i'm not sure if better or not.
try this, don't need pass id
function, instead can pass this
(refers element clicked),
function ryanclicked(el) { el.style.color = "blue"; }
<div id="sizebutton1"class="sizebutton" onclick="ryanclicked(this)">s</div> <div id="sizebutton2"class="sizebutton" onclick="ryanclicked(this)">m</div> <div id="sizebutton3"class="sizebutton" onclick="ryanclicked(this)">l</div>
Comments
Post a Comment