events - A way to unclick a button that has been clicked using Javascript -
is there way deselect or unclick button has been clicked using javascript?
so have button:
if (i === 10) { var clicked = document.getelementbyid('i10'); clickedbutton.push(click.textcontent); clicked.style.color = "pink"; }
so when click button turns text pink. there way click on button again , have remove push , turn text black?
sorry, javascript isn't strongest point.
web programming lesson time: if want set styles, don't use javascript set style, use css styling definitions, , use javascript point css.
in css:
.highlight { color: pink; background: blue; font-style: fantasy; whatever-else: stuffgoeshere; }
and button handling:
button.addeventlistener("click", function(evt) { var e = find.your.element.however.you.need(); e.classlist.toggle("highlight"); });
magic: doing things right way, code extremely straight forward, , we're not hardcoding styling, we're referring styling should defined.
"but if browser doesn't support .classlist?": reason because did not keep how browser have improved. every browser supports classlist
.
of course, if need more toggles, write function standalone operation, , throw elements @ it:
var records = {}; function togglehighlight(e) { e.classlist.toggle("highlight"); if (e.classlist.contains("highlight")) { // element highlighted, things accordingly: records[e.id] = e.textcontent; // ... } else { records[e.id] = false; // ... } } button.addeventlistener("click", function(evt) { var e = find.your.element.however.you.need(); togglehighlight(e); });
Comments
Post a Comment