I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url:
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
I have looked at a lot of examples on SO but can't seems to see what the issue is with my code:
I have urls that have hash tags / jump to id values appended to them. I want to check if the url has a hash tag and use that value as a click event like i would do on the page when clicking a button.
example url: http://domain./#help-video-modal-tags
jquery code:
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo() {
alert('in the function');
}
jQuery(window.location.hash).click(showVideo);
}
});
current click event triggers function
showVideo();
current click event triggers function
?
– Satpal
Commented
Apr 22, 2014 at 13:17
Use this
jQuery(document).ready(function() {
if ( window.location.hash ) {
function showVideo(hash) {
alert('in the function, hash -> ' + hash);
}
jQuery(document).click(showVideo(window.location.hash));
}
});
If I remember correctly to trigger an event of certain element is:
$('elem').trigger('click');
So in this case:
$(document).ready(function() {
if ( window.location.hash ) {
$(window.location.hash).trigger('click');//and trigger the event when needed
}
});
function showVideo() {
alert('do something when click is trigger');
}
$('elem').click(showVideo); //assign the event function normally