/    Sign up×
Community /Pin to ProfileBookmark

Event parameter in onclick function

In the first ‘Try it Yourself” example of the w3 tutorials
( See: [url]https://www.w3schools.com/howto/howto_js_tabs.asp[/url] )

The tab buttons are assigned an onclick function like this

[code]
<div class=”tab”>
<button class=”tablinks” onclick=”openCity(event, ‘London’)”>London</button>
<button class=”tablinks” onclick=”openCity(event, ‘Paris’)”>Paris</button>
<button class=”tablinks” onclick=”openCity(event, ‘Tokyo’)”>Tokyo</button>
</div>
[/code]

I can not find a description of the ‘event’ attribute of the function
and I do not see it being used in the ‘openCity’ function being called.

[code]
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;

// Get all elements with class=”tabcontent” and hide them
tabcontent = document.getElementsByClassName(“tabcontent”);
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = “none”;
}

// Get all elements with class=”tablinks” and remove the class “active”
tablinks = document.getElementsByClassName(“tablinks”);
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(” active”, “”);
}

// Show the current tab, and add an “active” class to the button that opened the tab
document.getElementById(cityName).style.display = “block”;
evt.currentTarget.className += ” active”;
}
[/code]

Could someone give me an explanation of how ‘event’ is being used by the function
or a reference that describes it use within the function?

Or how is is used elsewhere?
Does it relate somehow to the ‘this’ attributes of object functions?
?
I’m totally confused as to how it is being used in a function that does not seem to use it!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rootSep 11.2017 — Doesn't appear to be used at all, it might be just there because it was code used in an example that does use the event object.
Copy linkTweet thisAlerts:
@JMRKERauthorSep 11.2017 — Seems my mis-understanding of the problem is related to the 'evt.currentTarget'

Looked at that for additional information and found event attributes of '.target' and '.currentTarget'

Further investigation lead to this blog.

See: http://www.qc4blog.com/?p=650

Addiitonally, I discovered that a class can be defined as part of an HTML tag.

Before this, I thought that the class had to be defined as a standalone definition, ie'
<i>
</i> .someClass { background-color: yellow; }
as opposed to
button.someClass { background-color: yellow; }

So much to discover and remember, so little time for both!. :eek:
Copy linkTweet thisAlerts:
@rootSep 11.2017 — Yes, you can target HTML elements and also elements by their ID, so you can have a general CSS rule for DIV elements but when you have a div element with an ID tag that you want a special rule for, you can apply that rule to that DIV only or input, etc.
Copy linkTweet thisAlerts:
@BugsByteDec 06.2019 — I found a video related to this which might help you to do it in easier manner.

link: https://www.youtube.com/results?search_query=javascript+tab

and I formated the resets on top and tab format on bottom so that it would be easier to understand like in below link

https://codepen.io/BlazingIce/pen/OJPygrB
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...