Sometimes, you can build a UI using jQuery, many of which are already made for us for the efficiency of the work on jQuery. In this post, we will cover how we could build the UI with jQuery and also compare it to CSS. 

http://jqueryui.com

 

jQuery UI

jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQue

jqueryui.com

This website is a website where you can explore more information about the UI with jQuery.

 

Draggable

 

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#draggable" ).draggable();
  } );
  </script>
</head>
<body>
 
<div id="draggable" class="ui-widget-content">
  <p>Drag me around</p>
</div>
 
 
</body>
</html>

Droppable

 

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Droppable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  } );
  </script>
</head>
<body>
 
<div id="draggable" class="ui-widget-content">
  <p>Drag me to my target</p>
</div>
 
<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>
 
 
</body>
</html>

Resizable

 

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Resizable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #resizable { width: 150px; height: 150px; padding: 0.5em; }
  #resizable h3 { text-align: center; margin: 0; }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#resizable" ).resizable();
  } );
  </script>
</head>
<body>
 
<div id="resizable" class="ui-widget-content">
  <h3 class="ui-widget-header">Resizable</h3>
</div>
 
 
</body>
</html>

Datepicker

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
  </script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
 
 
</body>
</html>

You can also use this simple code to show the calendar in JSP.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
 <input type=date>
</body>
</html>

 

 

Accordion 

 

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Accordion - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#accordion" ).accordion();
  } );
  </script>
</head>
<body>
 
<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>
    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
    suscipit faucibus urna.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
    mauris vel est.
    </p>
    <p>
    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
    inceptos himenaeos.
    </p>
  </div>
</div>
 
 
</body>
</html>

To view the source, you can click view source. 

Also, you can make them by using CSS/Bootstrap. Let me give you one example.

Accordion with Bootstrap

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Accordion Example</h2>
  <p><strong>Note:</strong> The <strong>data-parent</strong> attribute makes sure that all collapsible elements under the specified parent will be closed when one of the collapsible item is shown.</p>
 
  <div id="accordion">
  
    <div class="card">
      <div class="card-header">
        <a class="card-link" data-toggle="collapse" href="#collapseOne">
          	Java
        </a>
      </div>      
      <div id="collapseOne" class="collapse show" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </div>
      </div>      
    </div>
    
    <div class="card">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
       		Python
      </a>
      </div>
      <div id="collapseTwo" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </div>
      </div>
    </div>
    
    <div class="card">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
         	 Oracle
        </a>
      </div>
      <div id="collapseThree" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </div>
      </div>
    </div>
  </div>
</div>
    
</body>
</html>

'Javascript > jQuery' 카테고리의 다른 글

jQuery) To print a map on a browser  (0) 2022.08.25

When you enter some websites, you will see locations like the followings : 

Since Google offers Google maps, we can put the map on a browser of our websites. It used to be easier than now to use without any keys, but currently, to use it, you need to have the API Keys. Here is the link for the API Keys: https://developers.google.com/maps/documentation/embed/get-api-key

 

Use API Keys  |  Maps Embed API  |  Google Developers

Send feedback Use API Keys Google Maps Platform products are secured from unauthorized use by restricting API calls to those that provide proper authentication credentials. These credentials are in the form of an API key - a unique alphanumeric string that

developers.google.com

Since I already have the key, I will jump to the below codes to access Google maps on a browser.

<!DOCTYPE html>
<html>
<body>

<h1>My First Google Map</h1>

<div id="googleMap" style="width:100%;height:400px;"></div>

<script>
function myMap() {
var mapProp= {  
  center:new google.maps.LatLng(-33.8567844, 151.2152967),
  zoom:18,
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDEM3FEmY5ecJzAkXH9TDRAs1MaXpSWtME&callback=myMap"></script>

</body>
</html>

You can also make a button to load the center that you set. So, once users want to see your location, they can simply click the button. 

 

Map with a button

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDEM3FEmY5ecJzAkXH9TDRAs1MaXpSWtME"
  type="text/javascript"></script> -->
<script src="http://code.jquery.com/jquery-latest.js"></script>

<script src="http://maps.googleapis.com/maps/api/js"></script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDEM3FEmY5ecJzAkXH9TDRAs1MaXpSWtME"
  type="text/javascript"></script>

<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(37.497975, 127.027506),
    zoom:18,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);
}
//google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
	$(document).ready(function(){
		$("button").click(function(){
//			reload();
			initialize();
		});
	});

</script>

</head>

<body>
<button>Click to see map</button>
<div id="googleMap" style="width:500px;height:380px;"></div>

</body>
</html>

center : Gangnam Staion,&nbsp; zoom : 18
center: Gangnam Station, zoom : 15
center : Sagrada Familia, zoom: 15

To set the center, you need to know the longitude and the latitude. By copying and pasting them to the code, it can easily be set.

 

load event with DOM Level 2 method

Here, we used the addDomListener() and added a marker on the map. 

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js">
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDEM3FEmY5ecJzAkXH9TDRAs1MaXpSWtME"
  type="text/javascript"></script>
  
<script>
var myCenter=new google.maps.LatLng(62.4539464, -114.3704224);

function initialize(){
var mapProp = {
  center:myCenter,
  zoom:14,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
  position:myCenter,
  });

marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

To make a bouncy marker, edit the code as the following: 

var marker=new google.maps.Marker({
  position:myCenter,
  animation:google.maps.Animation.BOUNCE 
  });

 

Bouncing marker

A marker with a title

You can see the title once you hover the mouse, and when you click the pin, it will zoom. 

var marker = new google.maps.Marker({
  position: myCenter,
  title:'Click to zoom'
  });

marker.setMap(map);

// Zoom to 9 when clicking on marker
google.maps.event.addListener(marker,'click',function() {
  map.setZoom(20);
  map.setCenter(marker.getPosition());
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

 

'Javascript > jQuery' 카테고리의 다른 글

jQuery UI  (0) 2022.08.25

Ajax (Asynchronous JavaScript + XML)

Ajax is an Asynchronous communication processing using JavaScript and XML. Ajax is used to send and receive data to and from the server without re-reading the entire web page with JavaScript. JavaScript is used to get data from the server and use it to get data from the whole page. It is possible to change only certain parts without refreshing, and Ajax makes this possible.


The web browser takes an HTML file from the server to display the web page, interprets the HTML, and sprays it on the screen. For example, a user imports new information to update the entire web page I am doing. This process takes time for the browser to read and interpret HTML from the server and display it on the screen. Still, with Ajax, only a portion of the page is available, so the ability to renew allows for much more flexibility in creating websites and increasing processing speed and user usability.

 

Here are some essential and main functions that are used in Ajax. 

 

$.ajax()
    $.get(): Performs Ajax in the get method.
    $.post(): Performs Ajax in the post method.
    $.getJSON(): Performs Ajax in the get method to obtain JSON data.
    $.getScript(): Performs Ajax to get the Script data and bring it.
    $(selector).load(): After performing Ajax, the file (string) is recalled to be answered in the selected document object.    

 $.ajax({ 
   URL: "Transfer Page",
   type: "Transfer method (get, post method),
   data: "Data to be transferred",
   dataType: "Requested data type"("html",xml",json",text",
   success : function(result){
        Sentence to be executed if the transfer is successful;
             }
    });

 

Here is an example of load() method. 

 

load() method 01

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>load() method</title>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

<script type="text/javascript">
$(function() {	
	$('button').click(function() {
		$('#container').load('resource.html');
	});   
});
</script>

</head>
<body>
	<button> Bring data from server </button>
	<div id="container"> Before </div>
</body>
</html>
<!--resource.html -->
<b> Happy New Year !!</b>

Output

When you click the button, you will see the following: 

load() method 02

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>load() meathod -2</title>
<style type="text/css">
div {
  width: 180px;  height: 80px;
  margin: 3px;   float: left;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">

$(function() {	
	   $('#menu1').click(function () {           
		  	 $('#message1').load('menu.html');     
		  	 return false;						   
		});
	   
	   $('#menu2').click(function () {          
		  	 $('#message2').load('menu.html li'); 
			 return false;                        
		});
});
</script>
</head>
<body>
<div>
	<a href="#" id="menu1">Menu1</a><p>
	<span id="message1"></span>
</div>
<div>
	<a href="#" id="menu2">Menu2</a><p>
	<span id="message2"></span>
</div>	
</body>
</html>
<!-- Menu.html -->
<p> Menu </p>
<ul>
	<li> Pizza </li>
	<li> Pasta </li>
	<li> Pork </li>
	<li> Mushroom Soup </li>
</ul>
<p> Please choose one menu.</p>

Output

JSON (JavaScript Object Notation)

JSON replaces XML data. It is a structure that has a key and a value in pairs. When using an array, combine it in square brackets ([ ]) using curly brackets ({ }).
In jQuery, you can save the data expressed in JSON to a file and then load it when needed.Give the getJSON() function.

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON</title>
<style>
td {
  border: 1px solid gray;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
/*
	Instead of the $.getJSON(), the $.ajax() can be used to obtain data in JSON format.

*/

$(function() {
    $.ajax({
         url : "item.json",
         dataType : "json",
         success : function(data) {
             $("#treeData").append(
                    "<tr><td>id</td>" + "<td>name</td>" 
                  + "<td>price</td>" + "<td>description</td>" + "</tr>");
             $.each(data, function() {
                 $("#treeData").append("<tr>" + "<td>"                 
                         + this.id + "</td>" + "<td>"
                         + this.name + "</td>" + "<td align='right'>"
                         + this.price + "</td>" + "<td>"
                         + this.description + "</td>" + "</tr>");
             });
         }
    });
});

</script>
</head>
<body>
  <table id="treeData"></table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON 02</title>
<style>
td {
  border: 1px solid gray;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
/*  
	- To make a get-type request from the server and receive a response in JSON format, $.Use the getJSON().
	- The first parameter of the $.getJSON() specifies the URL address of the server. ('item.json')
	- When the request to the server is successfully completed, the callback function described by the second parameter is called. (function(data, textStatus))
	  The first parameter of the callback function, data, is the JSON object type data returned from the server, and the second parameter, textStatus, is successful
	  The string "success" is delivered.

*/

$(function() {
	
	$.getJSON('item.json', function(data, textStatus) {
//		alert(data);
//		alert(textStatus);		success
        $("#treeData").append(
                 "<tr><td>id</td>" + "<td>name</td>" 
               + "<td>price</td>" + "<td>description</td>" + "</tr>");
        $.each(data, function() {
              $("#treeData").append("<tr>" + "<td>"                 
                      + this.id + "</td>" + "<td>"
                      + this.name + "</td>" + "<td align='right'>"
                      + this.price + "</td>" + "<td>"
                      + this.description + "</td>" + "</tr>");
          });
	 });
	
});

</script>
</head>
<body>
  <table id="treeData"></table>
</body>
</html>

item.json

[
  {
    "id": "1",
    "name": "Lemon",
    "price": " 3000",
    "description": "Cuenic acid in lemon is good for fatigue recovery. Vitamin C is also abundant."
  },
  {
    "id": "2",
    "name": "Kiwi",
    "price": " 2000",
    "description": "It is very rich in vitamin C. It is also very good for diets and beauty."
  },
  {
    "id": "3",
    "name": "Blueberry",
    "price": " 5000",
    "description": "Anthocyanin in blueberries is effective for eye fatigue."
  },
  {
    "id": "4",
    "name": "Cherry",
    "price": " 5000",
    "description": "Cherry has a lot of sweet taste and is good for fatigue recovery."
  },
  {
    "id": "5",
    "name": "Melon",
    "price": " 5000",
    "description": "Melon contains a lot of vitamin A and potassium."
  },
  {
    "id": "6",
    "name": "Watermelon",
    "price": "15000",
    "description": "It is a fruit rich in water."
  } 
]

Output

post()

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>To load jsp file to $.ajax</title>
<style>
td {
  border: 1px solid gray;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>

<script type="text/javascript">
/*
    - $.post() is a jQuery function that communicates with a server in the post manner.
    - $.post() requests the welcome.jsp page of the server in a post manner.
*/

$(function() {
    $('#submit').click(function () {
            var username = $('.username').val();	
            var sendData = 'username=' + username; 
            
            /* $.post(									
                 "welcome.jsp",						
                  sendData,        
                  function (msg) {				->callback function 
                	alert(msg);
            	    $('#message').html(msg);		
                });    */
            
            	$.ajax({
            		url :"welcome.jsp",
            		type :"post",
            		data : {"username": username},
            		success : function(msg){
            			alert(msg);
            			$('#message').html(msg);
            		} 
            	}); 
            
            return false;      
    });
});

</script>
</head>
<body>
    <form>  
    <label> Insert your name : </label>  
    <input type="text" name="username" class="username"/>  <br/>
    <input type="button" id="submit"  value="Submit"/>  
  </form>  
  <div id="message"></div>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 Welcome, <%=request.getParameter("username")%>. <br> <!--Expression Tag -->
 Welcome, ${param.username}.  <br> <!-- EL : Expression language-->
</body>
</html>

Output

getScript()

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>To load script</title>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">


  $(document).ready(function() {

    $.getScript("test.js");				
    $('#submit').click(function() {
    	var msg=call($('.username').val());	
    	$('#message').html(msg);			
    	 return false;
    });

  });

</script>
</head>
<body>
  <form>  
    <label> Insert your name : </label>  
    <input type="text" name="username" class="username"/>  <br/>
    <input type="button" id="submit" value="Submit"/>
    <div id="message"></div> 
  </form>  
</body>
</html>

test.js

function call(param){
	return ("Hello, " + param);
}

Output

 

+ Recent posts