I’ve tried all the combinations I could think about and didn’t figure it out. Here’s my working code (all in the HTML document):
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var $alert = $('.alert');
if ($alert.length) {
var alerttimer = window.setTimeout(function () {
$alert.trigger('click');
}, 3000);
$alert.animate({ height: $alert.css('line-height') || '50px' }, 200).click(function () {
window.clearTimeout(alerttimer);
$alert.animate({ height: '0' }, 200);
});
}
});
</script>
And here’s the call:
<div id="alert"><a href="../Home/English">English</a></div>
Now I want to refactor so the script goes in a .js file. How would it look like once refactored? Thanks!
Just put your custom code inside a
.jsfile, upload it to your server, and reference it below the jQuery Core include:and then inside the
custom.jsfile you put your custom JS:Update
I just noticed that your sample HTML has an element with the ID of
alertand your JS code is searching for elements with the CLASS ofalert. If you want to target an ID then you prepend a hash (#), not a period:Should change to:
You will probably want to bone-up on jQuery selectors (it will save you a lot of time in the future): http://api.jquery.com/category/selectors/