Event click doesn't seem to work in ruby's orbit


username

I have the following question. I created a clase .save and then added event click to it but it didn't work

code

 <!DOCTYPE html>
<html>
  <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
        $("#bt").click(function(){
          $("form").append("<br><label>Add Province:</label>");
          $("form").append("<input type='text'/>");
          $("form").append("<input class='save' type='button' value='Guardar'/>");             
        });
        $(".save").click(function(){
          $(this).css("background-color","red");
        });
  });
  </script>
  </head>
  <body>
    <form id="form" action="#">
       <button id="bt">Add Province</button>

    </form>
  </body>
</html>
Ariel Alvarado

I think you need to prevent the default action from the button.

e.preventDefault();

The click event should also be

$("form").on("click",".save", function(){
          $(this).css("background-color","red");
});

This is because the button is dynamically added to the form. The click event can only be added to an element that already exists.

A small example http://jsfiddle.net/r2qx3874/2/

Related


Event click doesn't seem to work in ruby's orbit

username I have the following question. I created a clase .save and then added event click to it but it didn't work code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

Event click doesn't seem to work in Ruby on Rails

username I have the following question. I created clase .save file and then added it to the event but nothing works code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

Event click doesn't seem to work in Ruby on Rails

username I have the following question. I created clase .save file and then added it to the event but nothing works code <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script>

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

jQuery click event doesn't seem to work

Akahne Saikyo I'm practicing making a simple calculator using jQuery, and I think the code itself is pretty simple, but no matter what I do, absolutely nothing happens, as if there's no js file linked to beging. Here is the jQuery code: function ud(n){

jQuery's click event doesn't work

username I can't find the reason why my code is not working. When I click on the generated element, the alert doesn't fire. Here is a fiddle : http://jsfiddle.net/pZAdP/ and code <button id="addMenuItem">Add Menu Item</button> <div id="content"></div> functio

jQuery's click event doesn't work

Alvin Karimi I wrote this code, I need to use jquery to handle click event, but it doesn't work in any browser, when I click on any element, nothing happens. div has no z-index in CSS; The vulnerability "news" element is dynamically added this is the html code

jQuery's click event doesn't work

username I can't find the reason why my code is not working. When I click on the generated element, the alert doesn't fire. Here is a fiddle : http://jsfiddle.net/pZAdP/ and code <button id="addMenuItem">Add Menu Item</button> <div id="content"></div> functio

jQuery's click event doesn't work

username I can't find the reason why my code is not working. When I click on the generated element, the alert doesn't fire. Here is a fiddle : http://jsfiddle.net/pZAdP/ and code <button id="addMenuItem">Add Menu Item</button> <div id="content"></div> functio

The click event on the button doesn't seem to fire

Tim B I'm no stranger to HTML or JavaScript, although I don't use them very often. However, I'm very new to PHP and am currently facing a strange issue when trying to get a click event on a button embedded in HTML in PHP: <!DOCTYPE html> <html lang="de"> <hea

The click event on the button doesn't seem to fire

Tim B I'm no stranger to HTML or JavaScript, although I don't use them very often. However, I'm very new to PHP and am currently facing a strange issue when trying to get a click event on a button embedded in HTML in PHP: <!DOCTYPE html> <html lang="de"> <hea

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong.I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component that

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong. I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component tha

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong. I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component tha

Calling the event handler doesn't seem to work

coder captain I have code for collapsible item. var coll = document.getElementsByClassName("collapsible"); for (var i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var

Invoking input's click event doesn't work in Safari

super hornet I have an input, file type, with display:nonea button. When the button is clicked, the input event should fire. In IE, Chrome and Firefox it works but not in Safari! var elem=$('<input id="ajxAttachFiles" name="fileUpload" type="file" style="displ

jQuery's click event adding class doesn't work

Divyesh jesadiya In my project I have menus, I give them the id of the submenus, it's in header.php <div class="top-menu"> <ul> <nav class="cl-effect-13"> <li><a href="about.php" id="about">About</a></li> <li><a href="product.php" id="product">

Invoking input's click event doesn't work in Safari

super hornet I have an input, file type, with display:nonea button. When the button is clicked, the input event should fire. In IE, Chrome and Firefox it works but not in Safari! var elem=$('<input id="ajxAttachFiles" name="fileUpload" type="file" style="displ

jQuery's image click event doesn't work

this and that I want to trigger a JQuery event that shows a textbox when an image on the page is clicked. The HTML code of my page is: <body> <div id="left_box"> <ul id="thumbnails"> <li> <div class="photocontainer" width="100%"> <img class="photo" id="websi

Click event doesn't work in Chrome extension's "background" page

Volatile 3 I have added a background page with a form with buttons. I am using jQuery to handle DOM and events. On hitting the search button, it doesn't work as expected, it should show the alert("Processor. Click() invoked."); Update : On the console, I get:

Invoking input's click event doesn't work in Safari

super hornet I have an input, file type, with display:nonea button. When the button is clicked, the input event should fire. In IE, Chrome and Firefox it works but not in Safari! var elem=$('<input id="ajxAttachFiles" name="fileUpload" type="file" style="displ

jQuery's click event adding class doesn't work

Divyesh jesadiya In my project I have menus, I give them the id of the submenus, it's in header.php <div class="top-menu"> <ul> <nav class="cl-effect-13"> <li><a href="about.php" id="about">About</a></li> <li><a href="product.php" id="product">

jQuery's image click event doesn't work

this and that I want to trigger a JQuery event that shows a textbox when an image on the page is clicked. The HTML code of my page is: <body> <div id="left_box"> <ul id="thumbnails"> <li> <div class="photocontainer" width="100%"> <img class="photo" id="websi

jQuery's image click event doesn't work

this and that I want to trigger a JQuery event that shows a textbox when an image on the page is clicked. The HTML code of my page is: <body> <div id="left_box"> <ul id="thumbnails"> <li> <div class="photocontainer" width="100%"> <img class="photo" id="websi

Click event doesn't work in Chrome extension's "background" page

Volatile 3 I have added a background page with a form with buttons. I am using jQuery to handle DOM and events. On hitting the search button, it doesn't work as expected, it should show the alert("Processor. Click() invoked."); Update : On the console, I get:

Invoking input's click event doesn't work in Safari

super hornet I have an input, file type, with display:nonea button. When the button is clicked, the input event should fire. In IE, Chrome and Firefox it works but not in Safari! var elem=$('<input id="ajxAttachFiles" name="fileUpload" type="file" style="displ

click event click doesn't work

Alifur Islam History and homepage are working fine but 'ng-click="toggleMenu()"' is not working so please help me click event is ng-click="toggleMenu()"not working. Can someone help me out? my code is <ion-tab title="History" icon-off="ion-document" icon-on="i

click event click doesn't work

Alifur Islam History and homepage are working fine but 'ng-click="toggleMenu()"' is not working so please help me click event is ng-click="toggleMenu()"not working. Can someone help me out? my code is <ion-tab title="History" icon-off="ion-document" icon-on="i