i made a test.html document to test a script. Somehow it is not working and i can\'t get why nothing is happening. The Script is in -tags and wrapped with -tag and the css a
You are executing the JavaScript before the tag loads, which contains your
elements.
To solve this problem, you have three choices :
1.You can insert the JavaScript code you have written inside a $(document).ready()
callback. (see http://www.w3schools.com/jquery/event_ready.asp for more information)
Code :
$(document).ready(function () {
$('#menu li a').on('click', function () {
$('li a.current').removeClass('current');
$(this).addClass('current');
});
});
2.You can use the JavaScript's built in window.onload
function. (see https://thechamplord.wordpress.com/2014/07/04/using-javascript-window-onload-event-properly/ for more information)
Code:
window.onload = function () {
$('#menu li a').on('click', function () {
$('li a.current').removeClass('current');
$(this).addClass('current');
});
}
3.Place your tag before
tag in your page.
Code:
Note: Using
$(document).ready()
is a better option than usingwindow.onload
. (see window.onload vs $(document).ready() for more information)