dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript question

  1. #1
    Join Date
    Nov 2011
    Posts
    22

    Javascript question

    Hello there.



    I am looking for help with my code.



    I am trying to create a code that will change for instance the color of a selected text, only the text that I select and click on the red color button will be changed into red color.

    How can I do that ?

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    This will change all the text that has the class "text" to black, then change the selected text to red. If you want to change the background colour, then it would be x.style.backgroundColor = ... .
    HTML Code:
    <!DOCTYPE html>
    <html>
    	<head>
    		<script type = "text/javascript">
    			function change(x) {
    				var text = document.getElementsByClassName("text");
    				for(var i = 0; i < (text.length); i++) {
    					text[i].style.color = "#000000";
    					x.style.color = "#FF0000";
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<div id = "textContainer">
    			<p onClick = "change(this)" class = "text">Apple</p>
    			<p onClick = "change(this)" class = "text">Orange</p>
    			<p onClick = "change(this)" class = "text">Banana</p>
    		</div>
    	</body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles