This question already has an answer here:
I have printed an echo with php of input element and I need to get click event id using jquery. Below is the code I am trying to get
API.php(echo element to index.php using ajax):
echo"<input type='submit' id='something' value='accept me'/>";
index.php(need to get the id='something' in click event):
$("#something").click(function(){
alert("hi");
//doesnt work
});
why it is not working? Please suggest
</div>
Write onclick()
function on input
itself. It will work.
For more info, click Add Onclick On Submit Button - Stack Overflow
<form ... >
.
.
<input type='submit' id='something' onclick="return showValue();" value='accept me'/>
</form>
<script>
function showValue() {
alert('hi');
return false;
}
</script>
window.onload=function(){
$("#something").click(function(){
alert("hi");
});
}
Try this:
$( document ).ready(function() {
$("#something").click(function(){
alert("hi");
//doesnt work
});
});
if that does not work then you need to show your code. http://jsfiddle.net/y6bgov3e/
Here is running code:
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<input type='button' id='test' value='accept me'/>
<script>
$(document).ready(function(){
$('#test').bind('click', function(){
alert('Hello')
})
})
</script>
If you want to click on button then use button type insted of submit