Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-10-2013, 08:47 AM   PM User | #1
stonydallas
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
stonydallas is an unknown quantity at this point
How to style JavaScript?

I have this code that replaces text entered in a box
Code:
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
function replaceText(){
    // witth is not a spelling error, you cant
    // use with for a variable name
    // create array of replace objects
    var rep =[
        {replace : "cat", witth : "hat"}, 
        {replace : "dark", witth : "light"}
    ], // declare the rest of the variables
    i,
    // get value of the textarea
    text=document.getElementById("txt").value,
    // return text is now the value of the textarea
    returnText=text;
    // for every object in the rep (replace) array
    for(i=0;i<rep.length;i++){
        // replace "replace" with "witth" using
        // rep array
        returnText=returnText.replace(new RegExp(
          rep[i].replace,"igm"),rep[i].witth);
    }
    // set value of the text area
    document.getElementById("txt").value=returnText
}
</script>
</head>
<body >
<textarea id="txt">The dark cat.</textarea>
<input type="button" onclick="replaceText();" value="Replace"/>
</body>
</html>
Now I need to style it!!!
I really only need to center it and move the "replace" button below, but If you also know how i would like to have the text area black, the text inside the replace button white, the button background black, the button border square and the button border white!
I am a beginner to css and i'm very confused!
If you only know how to do one of the listed styles that's fine! If it only looks a bit better, well, that's an improvement!

Thanks for your time!

Last edited by stonydallas; 02-10-2013 at 09:27 AM..
stonydallas is offline   Reply With Quote
Old 02-10-2013, 09:29 AM   PM User | #2
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

Use CSS to style

sample:

Code:
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
function replaceText(){
    // witth is not a spelling error, you cant
    // use with for a variable name
    // create array of replace objects
    var rep =[
        {replace : "cat", witth : "hat"}, 
        {replace : "dark", witth : "light"}
    ], // declare the rest of the variables
    i,
    // get value of the textarea
    text=document.getElementById("txt").value,
    // return text is now the value of the textarea
    returnText=text;
    // for every object in the rep (replace) array
    for(i=0;i<rep.length;i++){
        // replace "replace" with "witth" using
        // rep array
        returnText=returnText.replace(new RegExp(
          rep[i].replace,"igm"),rep[i].witth);
    }
    // set value of the text area
    document.getElementById("txt").value=returnText
}
</script>
<style type="text/css">
#TAreaDiv {
	height: auto;
	width: 250px; /* sample size change to suit */
	border: thin dotted #00FF00;
}
#txt {
	position:relative;
	color: #FFFFFF; /* text white*/
	background: #000000; /* black */
	margin:0 auto;
	display: block;
}
#txtAreaBtn {
	position:relative;
	border:#FFFFFF thin solid; /* white */
	margin:0 auto;
	display: block;
	color: #FFFFFF; /* white */
	background: #000000; /* black */
}
</style>
</head>
<body >
<div id="TAreaDiv">
<textarea id="txt">The dark cat.</textarea>
<input type="button" id="txtAreaBtn" onClick="replaceText();" value="Replace"/>
</div>
</body>
</html>
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown

Last edited by low tech; 02-10-2013 at 09:51 AM..
low tech is offline   Reply With Quote
Users who have thanked low tech for this post:
stonydallas (02-10-2013)
Old 02-11-2013, 01:43 AM   PM User | #3
stonydallas
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
stonydallas is an unknown quantity at this point
Quote:
Originally Posted by low tech View Post

sample:

Code:
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
function replaceText(){
    // witth is not a spelling error, you cant
    // use with for a variable name
    // create array of replace objects
    var rep =[
        {replace : "cat", witth : "hat"}, 
        {replace : "dark", witth : "light"}
    ], // declare the rest of the variables
    i,
    // get value of the textarea
    text=document.getElementById("txt").value,
    // return text is now the value of the textarea
    returnText=text;
    // for every object in the rep (replace) array
    for(i=0;i<rep.length;i++){
        // replace "replace" with "witth" using
        // rep array
        returnText=returnText.replace(new RegExp(
          rep[i].replace,"igm"),rep[i].witth);
    }
    // set value of the text area
    document.getElementById("txt").value=returnText
}
</script>
<style type="text/css">
#TAreaDiv {
	height: auto;
	width: 250px; /* sample size change to suit */
	border: thin dotted #00FF00;
}
#txt {
	position:relative;
	color: #FFFFFF; /* text white*/
	background: #000000; /* black */
	margin:0 auto;
	display: block;
}
#txtAreaBtn {
	position:relative;
	border:#FFFFFF thin solid; /* white */
	margin:0 auto;
	display: block;
	color: #FFFFFF; /* white */
	background: #000000; /* black */
}
</style>
</head>
<body >
<div id="TAreaDiv">
<textarea id="txt">The dark cat.</textarea>
<input type="button" id="txtAreaBtn" onClick="replaceText();" value="Replace"/>
</div>
</body>
</html>
Thanks this works!
stonydallas is offline   Reply With Quote
Old 02-11-2013, 03:13 AM   PM User | #4
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

No problem.

To learn more about CSS, you could start here.

Go through the links on the left menu.

LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Reply

Bookmarks

Tags
css, html, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:13 PM.


Advertisement
Log in to turn off these ads.