when I click … I dont get print Test but only Test1
My question
function name in html snippet is gmntFunct . Is it correct syntax ? is not it should have been gmntFunct ()
I did not get the alert Test when I clicked on html… as you see click code is inside gmntFunct() …so it should call the alert Test defined in gmntFunct() . whats wrong ?
Note: other symbols (jquery ) / notations are standard & implied
That makes no sense with the given code. according to that it should do nothing when clicked.
it is correct syntax, but it doesn’t call the function. for that you’d indeed need the parentheses. but you should use an event listener rather than event attributes anyways.
since the function is not called when you click the link (which should really be a button) there is no alert to be expected.
no. an event attribute is not a statement. its value (i.e. gmntFunct) is a statement. and yes, just the function name is a valid statement, although a pointless one.
you don’t need the gmntFunct() at all. actually it only gets in the way since it adds the same event listener on each call, but you only want one listener.
The link assigns one event hander for the button, and that function also assigns another event handler for the button. Each time that you click the button, the Test1 part of the code will run an extra time.
The solution here is to move the event handler assignment outside of the function.
function gmntFunct() {
alert('Test');
}
$("#Btn").on('click', function() {
alert('Test1');
});
##Code smell #2: Inappropriate coupling - onclick
The inline onclick attribute is a problem because if the behaviour of the code needs to change, that requires you to edit both the html and the JavaScript. It’s better to attach event handlers from the script itself.
If you only want one event handler for the button, the above commented-out code is no longer required.
##Code smell 3: Inappropriate coupling - void()
Preventing the default behaviour used to be done with void() but that doesn’t belong in with the HTML code. Use JavaScript instead to prevent the default behaviour, and leave HTML alone to be just HTML.
One option to prevent the default is to update the parameters in the gmntFunct function to accept the event object.
function gmntFunct(evt) {
evt.preventDefault();
alert('Test');
}
$("#Btn").on('click', gmntFunct);
The other way which I prefer, is to use a separate handler function, which helps to keep separate this type of behaviour from the function that we want to use.
function gmntFunct() {
alert('Test');
}
function buttonClickHandler(evt) {
evt.preventDefault();
gmntFunct();
}
$("#Btn").on('click', buttonClickHandler);
##Code smell #4: Unused code - type attribute
The type attribute is only used on input element, not anchor elements, so that needs to be removed.
function gmntFunct() {
alert('Test');
}
function buttonClickHandler(evt) {
evt.preventDefault();
gmntFunct();
}
$("#Btn").on('click', buttonClickHandler);
Lastly, there are tools such as JSNose that can help to sniff out different cod smells, but it can be more useful to learn about them, such as from this JavaScript Code Smells talk video.