jQuery call a function after page loaded

This article uses jQuery to call a function after a page is loaded. Table of contents: 1. $(document).ready() 2. jQuery call a function after page loaded 3. References P.S Tested with jQuery 3.7.1 1. $(document).ready() In jQuery, the code inside the $(document).ready() method will run once when the Document Object Model (DOM) is ready, equivalent …

Read more

Spring Boot Ajax example

This article will show you how to use jQuery.ajax to send a HTML form request to a Spring REST API and return a JSON response. Tools used : Spring Boot 1.5.1.RELEASE Spring 4.3.6.RELEASE Maven 3 jQuery Bootstrap 3 Note You may interest at this classic Spring MVC Ajax example 1. Project Structure A standard Maven …

Read more

jQuery Ajax submit a multipart form

A simple jQuery Ajax example to show you how to submit a multipart form, using Javascript FormData and $.ajax() 1. HTML A HTML form for multiple file uploads and an extra field. <!DOCTYPE html> <html> <body> <h1>jQuery Ajax submit Multipart form</h1> <form method="POST" enctype="multipart/form-data" id="fileUploadForm"> <input type="text" name="extraField"/><br/><br/> <input type="file" name="files"/><br/><br/> <input type="file" name="files"/><br/><br/> <input …

Read more

Spring Boot file upload example – Ajax and REST

This article shows you how to upload files in Spring Boot web application (REST structure), using Ajax requests. Tools used in this article : Spring Boot 1.4.3.RELEASE Spring 4.3.5.RELEASE Thymeleaf jQuery (webjars) Maven Embedded Tomcat 8.5.6 Google Chrome Browser (Network Inspect) 1. Project Structure A standard Maven project structure. 2. Project Dependency Declares an extra …

Read more

jQuery – Ajax request return 200 OK but error event is fired?

Review a jQuery Ajax form submit. $(document).ready(function () { $("#hostingForm").submit(function (e) { e.preventDefault(e); var data = {} data["id"] = $("#id").val(); data["domain"] = $("#domain").val(); data["name"] = $("#name").val(); //… $.ajax({ type: "POST", contentType: "application/json", url: "{{home}}/hosting/update", data: JSON.stringify(data), dataType: ‘json’, timeout: 600000, success: function (data) { console.log("SUCCESS: ", data); }, error: function (e) { console.log("ERROR: ", e); …

Read more

Javascript – How to call function inside jQuery code

Review a Javascript code snippet to call a function which declared inside jQuery code : <script> //javascript function submitSearchForm() { updateErrorMessage("Please enter a website url"); } //jquery jQuery(document).ready(function($) { function updateErrorMessage(msg) { $(‘#error’).html(msg).hide().fadeIn(500); } } ); </script> But, browser console shows the updateErrorMessage function is not defined. Uncaught ReferenceError: updateErrorMessage is not defined Solution To …

Read more

jQuery loop over JSON string – $.each example

Review a simple jQuery example to loop over a JavaScript array object. var json = [ {"id":"1","tagName":"apple"}, {"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"}, {"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"} ]; $.each(json, function(idx, obj) { alert(obj.tagName); }); Above code snippet is working fine, prompts the “apple”, “orange” … as expected. Problem : JSON string Review below example, declares a JSON string (enclosed with single …

Read more

jQuery and Java List example

There is no direct way to iterate over a Java List with jQuery, see the following case study : Spring controller @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView getPages() { List<String> list = new ArrayList<String>(); list.add("List A"); list.add("List B"); list.add("List C"); list.add("List D"); list.add("List E"); ModelAndView model = new ModelAndView("somepage"); model.addObject("list", list); return model; …

Read more

Spring MVC – find location using IP Address (jQuery + Google Map)

In this tutorial, we show you how to find a location using an IP address, with the following technologies : Spring MVC frameworks. jQuery (Ajax Request). GeoLite database. Google Map. Review the tutorial’s flows A page with a text input and button. Type an IP address, and clicks on the button. jQuery fires an Ajax …

Read more

How to get JSF id via jQuery

See a simple JSF example : <h:form id="signup-form"> <h:inputText id="email" value="#{beanBean.email}" /> </h:form> It will generates following HTML code : <input id="signup-form:email" type="text" /> Uses jQuery selector to get the email id, but failed. <script> jQuery(document).ready(function($) { $(‘#signup-form:email’).checkEmailFormat(); }); </script> Solution This is a well-known problem to integrate JSF and jQuery – the colon “:” …

Read more

How to get last Tweet via jQuery

Twitter, provides many APIs to deal with Tweets. In this example we are going to learn how to use jQuery to get last tweet of a user. Twitter API Get the last tweet of user (change “username” in the url with Twitter username you want) http://search.twitter.com/search.json?q=from:username&rpp=1 For example, get last tweet from twitter’s user “mkyong”. …

Read more

Custom jQuery plugin and CSS to display and hide content

The requirement : We need to show a collapsable content when we clicked ‘Show’ button, and hide the collapsable content when we clicked ‘Hide’ button. The solution : Create a custom jQuery plugin. Demo : Clicks on the “show” button. Note Here’s the alternative solution – jQuery toggle() function. 1. Collapsable content Below is an …

Read more

jQuery toggle example to display and hide content

The requirement : We need to show a collapsable content when we clicked ‘Show’ button, and hide the collapsable content when we clicked ‘Hide’ button. The solution : jQuery toggle() function. Demo : Clicks on the “show” button. 1. Collapsable Content Below is an example of HTML for collapsable content: <section class="round-border"> <h2>Use jQuery toggle …

Read more

jQuery – watermark effect on text input

A simple jQuery example to show you how to implement a watermark text effect on an input field. 1. Code Snippets An email input field and css watermark class. <style type="text/css"> input.watermark { color: #999; } //light silver color </style> <label>Email : </lable> <input id="email" type="text" /> jQuery to apply the watermark effect on email …

Read more

Add maxlength on textArea using jQuery

The “maxlength” attribute is not supported in textArea, but you can use JavaScript to limit the length of textArea dynamically. HTML 5 The textArea “maxlength” attribute is supported in HTML 5, the problem is not all browsers support HTML 5, especially, the old IE. jQuery Example A jQuery example to implement the “maxlength” effect on …

Read more

jQuery mousemove() example

The mousemove() event is fire when the mouse moves inside the matched element, and keep fire the event for every single pixel mouse moves around the matched element. For example, a big 300 x 200 div box with an id of “bigbigbox”. <style type="text/css"> #bigbigbox{ margin:16px 16px 16px 48px; border:1px groove blue; background-color : #BBBBBB; …

Read more

jQuery show() and hide() example

jQuery show() and hide() are the most common used effect. show() – Display the matched elements. hide() – Hide the matched elements. Both methods are support duration as parameter : slow, fast, or exact milliseconds. If this parameter is omitted, the default 400 milliseconds is apply. Try it yourself <html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <style …

Read more

jQuery after() and insertAfter() example

Both jQuery after() and insertAfter() methods are doing the same task, add a text or html content after the matched elements. The major difference is in the syntax. For example, <div class="greyBox">I’m a ipman</div> <div class="greyBox">I’m a ipman 2</div> 1. $(‘selector’).after(‘new content’); $(‘.greyBox’).after("<div class=’redBox’>Iron man</div>"); 2. $(‘new content’).insertAfter(‘selector’); $("<div class=’redBox’>Iron man</div>").insertAfter(‘.greyBox’); Result Both methods above …

Read more

jQuery append() and appendTo() example

Both jQuery append() and appendTo() methods are doing the same task, add a text or html content after the content of the matched elements. The major difference is in the syntax. For example, <div class="box">I’m a big box</div> <div class="box">I’m a big box 2</div> 1. $(‘selector’).append(‘new text’); $(‘.box’).append("<div class=’newbox’>I’m new box by prepend</div>"); 2. $(‘new …

Read more

jQuery text() example

jQuery text() is used to get the contents of all the matched elements; While text(‘new text’) is used to replace or set the text contents of all the matched elements. For example, <div class="TClass">TEXT 1</div> <div class="Tlass">TEXT 2</div> 1. $(‘.TClass’).text() This will combine all the contents of the matched elements, get “TEXT 1 TEXT 2” …

Read more

jQuery wrap() example

jQuery wrap() is used to wrap HTML element around each of the matched elements. For example, <div class="innerBox">Ip man vs Iron man</div> <div class="innerBox">Ip man 2 vs Iron man 2</div> Wrap it with a div tag that contains a class name of ‘wrapBox’. $(‘.innerBox’).wrap("<div class=’wrapBox’></div>"); The new contents will become : <div class=’wrapBox’> <div class="innerBox">Ip …

Read more

jQuery Tutorial

jQuery tutorial with full example, including jQuery selectors, DOM traversing, common practice, event handling (form, browser, mouse ,keyboard), animations effects and Ajax stuff

jQuery prepend() and prependTo() example

Both jQuery prepend() and prependTo() methods are doing the same task, add a text or html content before the content of the matched elements. The major difference is in the syntax. For example, <div class="box">I’m a big box</div> <div class="box">I’m a big box 2</div> 1. $(‘selector’).prepend(‘new text’); $(‘.box’).prepend("<div class=’newbox’>I’m new box by prepend</div>"); 2. $(‘new …

Read more

jQuery fadeIn(), fadeOut() and fadeTo() example

jQuery comes with three handy methods to create the fading effect easily. fadeIn() – Display the matched elements with fade in effect. fadeOut() – Hide the matched elements with fade out / transparent effect. fadeTo() – Fading the matched elements to certain opacity. Above three methods are support duration as parameter : slow, fast, or …

Read more

jQuery mouseup() and mousedown() example

Both jQuery mouseup() and mousedown() events are self-explanatory, to verify the mouse button is pressed or released. Try it yourself <html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <style type="text/css"> #mouseup, #mousedown{ float:left; padding:8px; margin:8px; border:1px solid red; width:200px; height:150px; background-color:#999999; } </style> </head> <body> <h1>jQuery mouseup() and mousedown() examples</h1> <div id="mouseup"> <h2>mouseup</h2> Fire when mouse over this …

Read more

jQuery resize() example

jQuery resize() event is fire when the size of the browser is changed, and this event is only bind to $(window). $(window).resize(function () { $(‘#msg’).text(‘Browser (Width : ‘ + $(window).width() + ‘ , Height :’ + $(window).height() + ‘ )’); }); To get the browser’s width and height details, use $(window).width() and $(window).height(). Try it …

Read more

jQuery form events examples

jQuery comes with five common form events to handle the form elements’ action. 1. focus() Fire when an element is on focus. $("input,select,textarea").focus(function () { //do something }); 2. blur() Fire when an element is loses focus. $("input,select,textarea").blur(function () { //do something }); 3. change() Fire when an element value is changed, e.g update checkbox, …

Read more

jQuery toggleClass example

The toggleClass() method means if matched elements do not have the class name, then add it; if matched elements already have the class name, then remove it. Let see an example, a simple ‘p’ tag. <p>This is paragraph</p> Call $(‘p’).toggleClass(‘highlight’), it will add a highlight class to the ‘p’ tag. <p class="highlight">This is paragraph</p> Call …

Read more

jQuery html() example

jQuery html() is used to get the html contents of the first element of the matched elements; While html(‘new html content’) is used to replace or set the html contents of all the matched elements. For example, two div elements that contains same class name “AClass”. <div class="AClass">ABC 1</div> <div class="AClass">ABC 2</div> 1. $(‘.AClass’).html() This …

Read more

jQuery click() and dblclick() example

jQuery click() and dblclick() events are the most common used mouse events : click() – Fire when mouse single click on the matched element. dblclick() – Fire when mouse double clicks on the matched element. Try it yourself <html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <style type="text/css"> #singleClick, #doubleClick{ float:left; padding:8px; margin:16px; border:1px solid blue; width:150px; height:150px; …

Read more

jQuery keyboard events example

jQuery comes with three keyboard events to capture the keyboard activities – keyup(), keydown() and keypress(). keyup() – Fire when user releases a key on the keyboard. keydown() – Fire when user presses a key on the keyboard. keypress() – Fire when user presses a key on the keyboard. In general statement, the keydown() is …

Read more

How to highlight table row record on hover with jQuery

jQuery comes with a hover() mouse event to allow attach two event handlers to the matched elements, executed when the mouse enters and leaves the matched elements. $("#id").hover(A, B); A – function to call when the mouse enters the matched element. B – function to call when the mouse leaves the matched element. This is …

Read more

Different between mouseover() and mouseenter() in jQuery

In jQuery, both mouseover() and mouseenter() events are fire when the mouse enters the matched element. The only different is in the way of the “event bubbling” handle in child element, let’s see two scenarios : 1. NO child element If the matched elements have no child element, both mouseover() and mouseenter() events are work …

Read more