Shadowing Posted March 29, 2012 Share Posted March 29, 2012 For some reason when I use ajax to place a button on a page. I cant get another java/ajax script to read that the button was pushed. Anyone have any obvious idea as to why? When i hit the button the screen refreshes so thats how i know the javascript isnt reading the button. I can read a input form that is placed on the page just not a button. both scripts are like this one below. one for putting the button on the page and a simliar one for clicking on the button. $(document).ready(function() { $("#update_announcements").click(function(event) { event.preventDefault(); $.ajax({ url: "/System_Lords/ajax/announcements.php?functions=announcements_box", type: 'POST', dataType: 'json', data: { announcements: '<?php echo $announcements; ?>' }, success: function(response) { $('#announcements_box').html(response.announcements_box); $('#announcements_button2').html(response.announcements_button); } }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/259918-java-script-ignoring-a-button/ Share on other sites More sharing options...
trq Posted March 29, 2012 Share Posted March 29, 2012 Events are attached as your page first loads. Because this button didn't exist then, it won't get any events attached to it. You can work around this by using event delegation. See http://api.jquery.com/on/ Quote Link to comment https://forums.phpfreaks.com/topic/259918-java-script-ignoring-a-button/#findComment-1332259 Share on other sites More sharing options...
Shadowing Posted March 29, 2012 Author Share Posted March 29, 2012 Thanks thorpe I kinda thought that might be the reason but then i kept thinking why does the <textarea> get read and not the button. but yah the link you gave is saying thats the issue. Quote Link to comment https://forums.phpfreaks.com/topic/259918-java-script-ignoring-a-button/#findComment-1332316 Share on other sites More sharing options...
Shadowing Posted March 29, 2012 Author Share Posted March 29, 2012 Im starting to some what understand firebug now. been sitting here trying alot of things and for some reason my java script still ignores this button. whats crazy is i read the link you gave me and they talk about using "on" but yet i cant find a reference to "on" in any tutor sites i go too. Then someon told me to try using "live" there is only one change_announcements id on the page also. also i tried $("input#change_announcements") the button is type"submit" <input type='submit' name='change_announcements' id='change_announcements' value='Update2'> $("#change_announcements").live("click",function(){ event.preventDefault(); $.ajax({ Quote Link to comment https://forums.phpfreaks.com/topic/259918-java-script-ignoring-a-button/#findComment-1332432 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.