i have a strange problem, i’m binding an action to a button using the jQuery function (click), when i click the button nothing happens, how that come!, here’s the code i’m using: `
<script >
$( '#admin' ).live( 'pageinit',function(event){
$('#AddButton').click(function(){
alert("Clicked")
});
});
</script>
Assuming the button exists in the initial page source you need to either:
<script>block somewhere in the source after the button, or$(document).ready()handler.Otherwise the button’s html will not have been parsed by the browser when the JS runs so the button won’t be found.
If the button does not exist initially, i.e., you are adding it dynamically in response to some other event, then you can use:
…similar to what you’re already doing for your
adminelement and the handler will automatically apply to the button once it does exist.Note though that
.live()is deprecated from jQuery version 1.7 onwards so if using 1.7 you should use the.on()method instead, and even in older jQuery the.delegate()method is preferred. The API doco for.live()explains how to convert.