jQuery DOM Manipulations - 1
jQuery DOM Manipulations
Methods Introduction
Hello,
So far, we have seen what JQuery is used for and how to use the selector.
Now let's see the methods that allow us to manipulate the DOM.
text method
text() method
We use the text() method to access the content of an HTML element.
Use $ (selector).text() to read the content of an element.
In addition, the text method is used to update the content of an element. It is enough to pass the new content to the text method as an argument.
<h1 id="hello">Hello Coddy</h1>
<h2 id="target"></h2>
<script>
$(document).ready(function(){
/*
With the $ ('# hello') statement,
we selected the element with hello id
and set the element's content related
to the text method of this object to
the variable named result.
*/
var result = $('#hello').text();
/*
We find the element with the
target id in the DOM and assign the value
of the result variable to the
text method of this object.
*/
$('#target').text(result);
/*
As a result, we have assigned
the value of the h1 element
to the h2 element.
*/
});
</script>