0% found this document useful (0 votes)
9 views3 pages

JavaScript Lecture 6

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

JavaScript Lecture 6

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

String Formatting Functions

- JavaScript string is considered as an object.


- You can format the string object dynamically by using following methods:
Method Description
-------------------------------------------------------
fontcolor() Sets text color
fontsize() Sets text size
bold() Bold
italics() Italic
sup() Super Script
sub() Sub Script
toUpperCase() Convert all chars to uppercase
toLowerCase() Convert all chars to lowercase

Note : All string formatting methods render HTML.


fondcolor('red') = <font color='red'>
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<title>String</title>
<script>
function Verifyuser(){
var username = document.getElementById("UserName").value;
var userMsg = document.getElementById("userMsg");
if(username.length < 4){
userMsg.innerHTML = "Name too short".fontcolor('red');
}else{
userMsg.innerHTML = "Name Verified".fontcolor('green');
}
}
function ChangeCase(){
var username = document.getElementById("UserName").value;
document.getElementById("UserName").value = username.toUpperCase();
}
</script>
</head>
<body>
<dl>
<dt>User Name</dt>
<dd><input placeholder="Name Block Letters only" onblur="ChangeCase()"
type="text" onkeyup="Verifyuser()" id="UserName"></dd>
<dd id="userMsg"></dd>
</dl>
</body>
</html>

Formatting String using Styles


- JavaScript allows to define styles dynamically for any HTML element.
- Style attributes are referred in CamelCase.
- Style attributes are not applied to string, they are applied to element that
handles string.
Syntax:
userMsg.style.backgroundColor = "red";
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<title>String</title>
<script>
function Verifyuser(){
var username = document.getElementById("UserName").value;
var userMsg = document.getElementById("userMsg");
if(username.length < 4){
userMsg.innerHTML = "Name too short";
userMsg.style.color = "red";
}else{
userMsg.innerHTML = "Name Verified";
userMsg.style.color = "green";
userMsg.style.fontStyle = "italic";
}
}
function ChangeCase(){
var username = document.getElementById("UserName").value;
document.getElementById("UserName").value = username.toUpperCase();
}
</script>
</head>
<body>
<dl>
<dt>User Name</dt>
<dd><input placeholder="Name Block Letters only" onblur="ChangeCase()"
type="text" onkeyup="Verifyuser()" id="UserName"></dd>
<dd id="userMsg"></dd>
</dl>
</body>
</html>
Ex: You can apply effects by using CSS class
- You have to use the property "classname".
Ex:
<!DOCTYPE html>
<html lang="en">
<head>
<title>String</title>
<link rel="stylesheet" href="">
<script>
function Verifyuser(){
var username = document.getElementById("UserName").value;
var userMsg = document.getElementById("userMsg");
if(username.length < 4){
userMsg.innerHTML = "Name too short";
userMsg.className = "text-danger";
}else{
userMsg.innerHTML = "Name Verified";
userMsg.className = "text-success";
}
}
function ChangeCase(){
var username = document.getElementById("UserName").value;
document.getElementById("UserName").value = username.toUpperCase();
}
</script>
</head>
<body>
<dl>
<dt>User Name</dt>
<dd><input placeholder="Name Block Letters only" onblur="ChangeCase()"
type="text" onkeyup="Verifyuser()" id="UserName"></dd>
<dd id="userMsg"></dd>
</dl>
</body>
</html>

You might also like