JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

JQuery Clone() Method Example

May 2, 2014 by Krishna Srinivasan Leave a Comment

The clone() method is used to make duplicate copy of selected elements. Means it copies the matched elements as well as their descendant elements and text nodes. It copies child nodes, text and attributes of the selected elements. It clones matched DOM elements and select the clones. It is also useful for moving elements into other location in the DOM. It is convenient way to make deep copy of elements on the page.

JQuery Clone() Syntax

[code lang=”xml”]
$(selector).clone().childnode();
[/code]

It has parameter called childnode is the required parameter which include childnode, text attributes.

JQuery Clone() Example

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Clone Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Clone Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("<p>Welcome to JavaBeat!!!</p>").clone().insertBefore("h3");
});
});
</script>
<body>
<h3>Hello!!!</h3>
<button>Click Here</button>
</body>
</html>
[/code]

  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“button”).click(function()) line defines click event which occurs when button is clicked.
  • $(“Welcome to JavaBeat!!!”).clone().insertBefore(“h3”); line defines clone method used to make duplicate copy of selected elements. It inserts the line Welcome to JavaBeat!!! before the line Hello!!!.

JQuery Clone() Demo

When you run the above example, you would get the following output:

  • Run This Demo

JQuery Clone Method Example

The clone() method is also used to make duplicate copy of selected elements with appendTo() method as shown in the below example:

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Clone Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Clone Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("<strong>Welcome to JQuery!!!</strong>").clone().appendTo(".myappend");
});
});
</script>
<body>
<button>Insert element to the end of p element</button>
<div class="myappend">
<p>Hello World!!!</p>
</div>
</body>
</html>
[/code]

In above example, we have used clone() method with appendTo() method which will make deep copy of selected elements. The appendTo() method indicates JS interpreter to append the content in quotes to div class elements. When you run the script, click on the button which will appear on the browser, the line Welcome to JQuery!!! appends after the line Hello World!!!

Filed Under: jQuery Tagged With: JQuery Methods

JQuery Attr() Method Example

May 2, 2014 by Krishna Srinivasan Leave a Comment

Jquery attr() method is used to retrieve the value of an attribute of the selected elements. It gets attribute for the element in the matched set. It sets attributes and values of selected elements and also sets one or more attributes for every matched element. It is also used to get attributes from the HTML tags. It is convenience method which can be called directly on a jQuery method and can be linked to other jQuery methods.It’s also possible to set multiple attributes and values using attr() method.

JQuery Attr() Syntax

[code lang=”xml”]
$(selector).attr(attribute);
[/code]

It has parameter called attribute is the required parameter which gets the value of attribute.

JQuery Attr() Example

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Attr Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Attr Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").attr("align","center");
});
});
</script>
<body>
<p>Welcome to JQuery!!!</p>
<button>Click Here</button>
</body>
</html>
[/code]

  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“button”).click(function()) line defines click event which occurs when button is clicked.
  • $(“p”).attr(“align”,”center”); line defines attr method which returns value of selected elements. It displays the line Welcome to JQuery!!!center on the browser.

JQuery Attr() Example Demo

When you run the above example, you would get the following output:

  • Run Demo Example

AttrMethod

We can also set multiple attributes and values using attr() method as shown below:

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Attr Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Attr Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("img").attr({width:"150",height:"100"});
$("#message").text("image width:"+$("img").attr("width") + "height:"+$("img").attr("height"));
});
});
</script>
<body>
<img src="C:\Sunset.jpg" width="250" height="250"><br>
<button>Click to Set Width and Height</button>
<p id="message"></p>
</body>
</html>
[/code]

The above example displays the image when we click on the button using attr() method. The line $(“img”).attr({width:”150″,height:”100″}) defines attr method which returns value of selected elements. Before clicking button, the width and height of the image was 250px respectively. When we click the button, image would change as specified in the attr() method arguments. It changes the width and height of the image and message will be printed with specified values of width and height.

When you run the above example, you would get the following output:

  • Run This Demo

AttrMethodExample

AttrMethodExample1

The attr() method also used to change the attribute value. The following example shows how to change value of the href attribute in a link:

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Attr Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Attr Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").attr("href","http://www.google.com");
});
});
</script>
<body>
<p><a href="http://www.google.com">Click on link to change href value</a></p>
</body>
</html>
[/code]

Above example shows how to change the value of href attribute in a link. when we execute the script, we will get link on the browser. The attr() method navigates to the web site which is specified in the arguments.

When you run the above example, you would get the following output:

  • Run This Demo

AttrMethodExample2

AttrMethodExample3

Filed Under: jQuery Tagged With: JQuery Methods

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved