-2

Alright so for some reason whenever I try to click this input nothing happens, altho it should alert hello world...

<label>Ime Profesora</label><br/>
<input type='text' id='profesor_Ime' placeholder='Ime profesora...' /><br/>
<input type='submit' id='profesor_Submit' value='Dodaj Profesora'/>

And the JS

$("#profesor_Submit").click(function() {
        alert("Hello world");
    });

Here is the Jsfiddle https://jsfiddle.net/8y0hq6md/1/

EDIT: I am loading the form dynamically from the SQL, could that maybe be the cause of the problem?

Tomislav Tomi Nikolic
  • 608
  • 3
  • 10
  • 15
  • Works fine when you add jQuery -> https://jsfiddle.net/8y0hq6md/2/ – adeneo Jan 13 '17 at 23:10
  • 1
    You need to include JQuery in that fiddle. – Tyler Sebastian Jan 13 '17 at 23:11
  • Yeah I forgot to include the Jquery into the code, altho one thing I suppose thats causing this is maybe because I am loading the form dynamically from the SQL or something? Not sure.. – Tomislav Tomi Nikolic Jan 13 '17 at 23:19
  • If you load something dynamically from ajax for example, you need to start searching from an element that is actually inside the DOM, for example: `$("body").on("click", "profesor_Submit", function() {` – Naruto Jan 13 '17 at 23:20
  • @TomislavTomiNikolic With your last comment... [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) and/or [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Jonathan Lonowski Jan 13 '17 at 23:22
  • Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Sebastian Simon Jan 13 '17 at 23:23
  • Possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Jecoms Jan 13 '17 at 23:47

3 Answers3

1

Make sure that the jQuery Library is included.

If that's already the case, also make sure that the document is "ready" by using $(document).ready().

Here's an example:

$(document).ready(function() {

   // Your code here

});

..or the shorthand version:

$(function() {

   // Your code here

});
TheYaXxE
  • 4,196
  • 3
  • 22
  • 34
0

You have not included jQuery library in your code.

$(document).ready(function() { 

  $("#profesor_Submit").click(function() {
    alert("Hello world");
  });

});
<label>Ime Profesora</label>
<br/>
<input type='text' id='profesor_Ime' placeholder='Ime profesora...' />
<br/>
<input type='submit' id='profesor_Submit' value='Dodaj Profesora' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

Updated jsFiddle: https://jsfiddle.net/8y0hq6md/10/

Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
-1

Okay so this was the solution

$(document.body).on('click', '#profesor_Submit' ,function(){
        alert("Hello world");
    });

This seems to work with it since I am loading the form dynamically

Tomislav Tomi Nikolic
  • 608
  • 3
  • 10
  • 15