943,172 Members | Top Members by Rank

Ad:
Sep 3rd, 2010
0

Dynamically Change Style

Expand Post »
Hey Guys,

I'm really stuck on how I can have the visitor change the style of the content on my webpage. I've looked into stylesheet switcher, but that changes a whole stylesheet, when really all I want users to be able to change is their preferred background color, alignment, font family, font color and font size.

So for example, how would I let the user be able to select a certain style type for these specific classes:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <table class="background">
  2. <tr class="justify">
  3. <td class="fontfamily">
  4. <p class="fontsize">
  5. <strong class="fontcolor">Hello World!
  6. </strong>
  7. </p>
  8. </td>
  9. </tr>
  10. </table>
So, I could have a script that would left the user control the class=Background, where they can select a background color from a selection.

I hope that makes sense! Can anyone help me out?
I really appreciate all your help!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brown23 is offline Offline
22 posts
since Sep 2010
Sep 3rd, 2010
0
Re: Dynamically Change Style
Found out how to do it!

If I'm honest then I didn't expect a reply because I couldn't really explain what I was trying to achieve! But, if anyone in future has the same problem, then here is the solution.

If you want to associate a different class when a user clicks something then here's how you do it. In this example, we're changing the background colour to black when the user clicks on a radio button:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Test</title>
  4. <style type="text/css">
  5. .blackbackground {background:#000000;}
  6. </style>
  7. <script type="text/javascript">
  8. function change(id, newClass) {
  9. identity=document.getElementById(id);
  10. identity.className=newClass;
  11. }
  12. </script>
  13. <body>
  14. <table id="background">
  15. <tr>
  16. <td><br/><input type="radio" name="styling" onclick="change('background', 'backgroundblack');">Black Background Background</td>
  17. </td>
  18. </tr>
  19. </table>
  20. </body>
  21. </html>

I'm sure you'll be able to work out how it works by looking at the code - you'll probably also be able to see how you can also manipulate the function to gain a better advantage!

If anyone has any problems please do reply and I'll explain a little bit more!

Thanks,
Brownie!
Last edited by brown23; Sep 3rd, 2010 at 8:03 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brown23 is offline Offline
22 posts
since Sep 2010
Sep 4th, 2010
0
Re: Dynamically Change Style
This works perfect for me:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Test</title>
  4. <style type="text/css">
  5. table.background {background:blue;}
  6. p {
  7. color: green;
  8. }
  9. </style>
  10. <script type="text/javascript">
  11. function change(id) {
  12. document.getElementById("background").style.backgroundColor = "white";
  13. document.getElementById("fonter").style.color = "black";
  14. document.getElementById("fontes").style.color = "black";
  15. }
  16. function changeb(id) {
  17. document.getElementById("background").style.backgroundColor = "black";
  18. document.getElementById("fontes").style.color = "white";
  19. document.getElementById("fonter").style.color = "white";
  20. }
  21. </script>
  22. <body>
  23. <table id="background" class="background">
  24. <tr>
  25. <td><br/><input type="radio" name="styling" onclick="change();"><p id="fonter">White Background</p></td>
  26. <td><br/><input type="radio" name="styling" onclick="changeb();"><p id="fontes">Black Background</p></td>
  27. </td>
  28. </tr>
  29. </table>
  30. </body>
  31. </html>
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
george61 is offline Offline
59 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: passing information between pages
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How do I "Javascript" on a Mac





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC