2

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

arch
  • 1,363
  • 2
  • 14
  • 30

4 Answers4

3

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>
Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Seems the OP doesn't know how things go on Stack. [I left them this...](http://stackoverflow.com/questions/34103171/click-event-on-jquery-does-not-work?noredirect=1#comment68057670_34103171) but seems to be to no avail. Oh well, what can you do. – Funk Forty Niner Nov 03 '16 at 14:21
1
 window.onload=function(){
         $("#something").click(function(){
           alert("hi");
         });
      }
Parth Patel
  • 774
  • 1
  • 8
  • 15
0

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/

Vineet1982
  • 7,730
  • 4
  • 32
  • 67
thepiyush13
  • 1,321
  • 1
  • 8
  • 9
0

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

Rahul Saxena
  • 465
  • 4
  • 15