friedice Posted August 30, 2012 Share Posted August 30, 2012 <a href="#" ><img src="img/sign_up_button.png" alt="signup" width="200px" height="40px"/></a> calls this js $(document).ready(function() { $('#slideleft a').click(function() { var $lefty = $('div.slideleft > div') $lefty.animate({ left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0 }); }); }); i have to click twice on the link to call that function? is there a reason why this happens? Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/ Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 Try this: $('#slideleft a').click(function(e) { e.preventDefault(); ... Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1373864 Share on other sites More sharing options...
friedice Posted August 31, 2012 Author Share Posted August 31, 2012 i tried using that but i still require 2 clicks to activate the js script i thot it would be something to do with the href inside the a tag since it called an empty url from it then calls the js, so i tried using button tag instead and still doesnt work >.< starting to frustrate me Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1374141 Share on other sites More sharing options...
codefossa Posted August 31, 2012 Share Posted August 31, 2012 The problem may be line var lefty = $('div.slideleft > div'); needing a delimiter? Anyways, here's a little easier to read code and maybe it will work now? $(document).ready(function() { $('#slideleft a').click(function(e) { e.preventDefault(); var lefty = $('div.slideleft > div'); lefty.animate({ left: parseInt(lefty.css('left'), 10) == 0 ? -lefty.outerWidth() : 0 }); return false; }); }); Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1374356 Share on other sites More sharing options...
nogray Posted September 1, 2012 Share Posted September 1, 2012 Your $lefty may not have the "left" css property set initially, try to alert your value and see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1374456 Share on other sites More sharing options...
friedice Posted September 2, 2012 Author Share Posted September 2, 2012 thanks it works now i forgot to set the left value initially in the css for the second div >.< Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1374594 Share on other sites More sharing options...
codefossa Posted September 2, 2012 Share Posted September 2, 2012 thanks it works now i forgot to set the left value initially in the css for the second div >.< You could just check if it's undefined then set it in the function, in case you wish to use this for multiple elements, then you wouldn't have to set them manually. Quote Link to comment https://forums.phpfreaks.com/topic/267792-have-to-click-twice-on-link-to-run-javascript-function/#findComment-1374652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.