Found 10483 Articles for Web Development

When to use $(document).ready() and when $(window).load() in jQuery?

Alex Onsman
Updated on 30-Jul-2019 22:30:20

867 Views

$(window).load()Use $(window).load(), when you want the code inside it to run only once the entire page is ready (not only DOM).  It executes when the page is fully loaded, including frames, objects and imagesNote: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0. To see its working, add jQuery version for CDN before 3.0.$(document).ready()Use the  $(document).ready() method when you want the code inside it to run once the page DOM is ready to execute JavaScript code.

How to iterate over arrays and objects in jQuery?

David Meador
Updated on 15-Jun-2020 13:17:04

1K+ Views

To iterate over arrays and objects in jQuery, use the jQuery forEach() loop. You can try to run the following code to learn how to iterate over arrays and objects −ExampleLive Demo $(document).ready(function(){   $("#button1").click(function(){     var arr = [{subject: "Maths", id:3}, {subject: "History", id:7}];     arr.forEach(function(el, index) {       alert(el.id+" "+el.subject);     });   }); });   Result

How to trim a string using $.trim() in jQuery?

Ricky Barnes
Updated on 15-Jun-2020 13:44:33

3K+ Views

To trim a string in jQuery, use the trim() method. It removes the spaces from starting and end of the string.The sample string I am using has spaces −var myStr = "  Hello World    ";Use the trim() method, jQuery.trim(myStr);The following is an example to trim a string in jQuery −ExampleLive Demo $(document).ready(function(){   $("#button1").click(function(){     var myStr = "  Hello World    ";     myStr = jQuery.trim(myStr);     alert(myStr);   }); }); Trim

How to check if onClick exists on element in jQuery?

ARPIT ANAND
Updated on 14-Feb-2020 08:01:05

804 Views

These codes might help you −$('body').click(function(){ alert('test' )}) var foo = $.data( $('body').get(0), 'events' ).click // you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.  $.each( foo, function(i,o) {     alert(i) // guid of the event     alert(o) // the function definition of the event handler });

How do I remove a property from a JavaScript object?

V Jyothi
Updated on 16-Jun-2020 06:21:09

184 Views

To remove a property from a JavaScript object, use the delete keyword. You can try to run the following code to learn how to remove a propertyExampleLive Demo                          var cricketer = {             name:"Amit",             rank:1,             points: 150          };          delete cricketer.rank;          document.getElementById("demo").innerHTML =             cricketer.name + " has " + cricketer.rank + " rank.";          

How to check if event exists on element in jQuery?

Kristi Castro
Updated on 19-Jun-2020 12:47:44

4K+ Views

To check if event exists on element in jQuery, check for the existing events on the element. Here, I have set the div −   This is demo text. Click here! When you click div, then the alert generates which I have set using the div id:$("#demo").click(function() {   alert("Does event exists? - "+hasEvents); });You can try to run the following code to check if event exists −ExampleLive Demo               $(document).ready(function(){       $("#demo").click(function() {         alert("Does event exists? - "+hasEvents);      });      var events = $._data(document.getElementById('demo'), "events");      var hasEvents = (events != null);    });         This is demo text. Click here!

How to enable and disable submit button using jQuery?

Kristi Castro
Updated on 20-Jun-2020 07:15:21

1K+ Views

To enable and disable submit button, use the jQuery prop() method. You can try to run the following code to enable and disable submit button using jQuery −ExampleLive Demo               $(document).ready(function() {       $('#register').click(function() {         $(this).prop('disabled',true);         $("#content").show();         setTimeout(function(){           $('#register').prop('disabled', false);         },3000);      });    });               The above button disables for 3 seconds, on clicking.                  

How can I use multiple submit buttons in an HTML form?

Bhanu Priya
Updated on 04-Oct-2023 15:20:10

13K+ Views

Generally, the forms in HTML uses a single submit button which saves the record when button is pressed. In some cases, we need a form with additional submit buttons such as "accept" or "reject". Those type of buttons usually attempt a state transition while updating the record. A form with multiple buttons has to process your server-side code needs to know which button was pressed. Generally, after submission the destination data is stored in the action attribute where every button leads to same location. To overcome this problem, we use formaction attribute. For each submit button we require a different ... Read More

Can I submit form with multiple submit buttons using jQuery?

Kristi Castro
Updated on 20-Jun-2020 07:14:15

3K+ Views

Yes, you can submit form with multiple submit buttons. Attack a custom click handler to all the buttons and the check which button is clicked.ExampleLive Demo               $(document).ready(function(){       $("#myform button").click(function (ev) {       ev.preventDefault()       if ($(this).attr("value") == "button1") {                     alert("First Button is pressed - Form will submit")         $("#myform").submit();       }       if ($(this).attr("value") == "button2") {         alert("Second button is pressed - Form did not submit")       }     }); });     First Button   Second Button

How to create div dynamically using jQuery?

Kristi Castro
Updated on 20-Jun-2020 07:13:16

2K+ Views

To create a div dynamically, use the html() method. You can try to run the following code to learn how to create a div dynamically on button click −ExampleLive Demo               $(document).ready(function(){       $("button").click(function(){         $("body").html("This is a new div.");       });     });   Create a new div This is demo text.

Advertisements