D3.js selection.text() Function
source link: https://www.geeksforgeeks.org/d3-js-selection-text-function/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
D3.js selection.text() Function
Last Updated: 23-08-2020The selection.text() function in d3.js is used to set the text content to the specified value of the selected elements thus, it replaces any existing child elements. If the value that is given is constant than all elements will be given that constant value.
Syntax:
selection.text([value]);
Parameters: This function takes only one parameter which is given above and described below:
- value: Text content value to set.
Return Values: This function does not return any value.
Example 1:
filter_none
edit
close
play_arrow
link
brightness_4
code
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
path1tent
=
"width=device-width,initial-scale=1.0"
>
</
script
>
</
head
>
<
body
>
<
h1
style
=
"color: green"
>GeeksforGeeks</
h1
>
<
div
>
<
button
>Click me</
button
>
</
div
>
<
script
>
function func() {
// Sets the text-content of the button
var chk = d3.select("button")
.text("This is the changed text");
var text = document.querySelector("button");
}
let btn = document.querySelector("button");
btn.addEventListener("click", func);
</
script
>
</
body
>
</
html
>
Output:
-
Before clicking the button:
-
After clicking the button:
Example 2:
filter_none
edit
close
play_arrow
link
brightness_4
code
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
path1tent
=
"width=device-width, initial-scale=1.0"
>
</
script
>
</
head
>
<
body
>
<
h1
style
=
"color: green;"
>GeeksforGeeks</
h1
>
<
div
style="background-color: green;
width: fit-content;
padding: 10px;
margin-top: 5px;"
class
=
"btn"
>
This text will be changed
</
div
>
<
div
style="background-color: green;
width: fit-content;
padding: 10px;
margin-top: 5px;"
class
=
"btn"
>
This text will be changed
</
div
><
br
>
<
br
>
<
button
class
=
"Clickme"
>Change text</
button
>
<
script
>
function func() {
// Selecting all buttons and
// Setting the text content of the button
var chk = d3.selectAll(".btn")
.text("This text is changed");
var text = document.querySelector("button");
}
let btn = document.querySelector(".Clickme");
btn.addEventListener("click", func);
</
script
>
</
body
>
</
html
>
Output:
-
Before clicking the button:
-
After clicking the button:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Be the First to upvote.
No votes yet.
first_page D3.js selection.property() Function last_page D3.js selection.merge() Function
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK