<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>Find Length of a String Using jQuery</
title
>
</
script
>
<
script
type
=
"text/javascript"
>
$(document).ready(function() {
$("button").click(function() {
var myStr = $("textarea").val();
if ($(this).hasClass("with-space")) {
var withSpace = myStr.length;
alert(withSpace);
} else if ($(this).hasClass("without-space")) {
var withoutSpace = myStr.replace(/ /g, '').length;
alert(withoutSpace);
}
});
});
</
script
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h1
style
=
"color:green;"
>
GeeksForGeeks
</
h1
>
<
h3
>Enter Text and Click the Buttons</
h3
>
<
textarea
rows
=
"5"
cols
=
"60"
>
How do you get the length of a string in JQuery?
</
textarea
>
<
br
>
<
button
type
=
"button"
class
=
"with-space"
>
Length of string including white-spaces
</
button
>
<
br
>
<
br
>
<
button
type
=
"button"
class
=
"without-space"
>
Length of string without white-spaces
</
button
>
</
body
>
</
html
>