Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2004, 08:57 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default changing style using javascript

Hi,

I'm having a problem changing the style of a form element, I've used the following script to validate the field on submit, and I want the element to be highlighted with a border if blank.

However it does not work, can anybody help?

-----------------------
if (document.personalDetails.surname.value == "")
    {
        alert("Enter a Surname");
        document.personalDetails.surname.style.border = "3px solid ##FF0000";
        return false;
    }
---------------------------------------------

 
Old September 20th, 2004, 09:24 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

One hash is enough, probably more than enough but I always use one:
Code:
document.personalDetails.surname.style.border = "3px solid #FF0000";
--

Joe
 
Old September 21st, 2004, 09:19 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great thanks for that.

Do you know how I could change the style back to its default?

AS I change the style here:

document.personalDetails.surname.style.border = "3px solid ##FF0000";

Do you know how I can set the text area back to how it was, with the default bevel?

Many thanks
 
Old September 21st, 2004, 09:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can save the setting before changing it:
Code:
oBorderStyle = element.style.border
and then put it back later. This page shows something along those lines, interestingly the order for setting the three properties seems irrelevant.
Code:
<html>
<head>
<title>StyleChange</title>
<script type="text/jscript">
  function showBorder(ElementId)
  {
    var oDiv = document.getElementById(ElementId);
    var oTxt = document.getElementById("txtShowBorder");
    oTxt.value = oDiv.style.border; 
  }

  function setBorder(ElementId, Border)
  {
    var oDiv = document.getElementById(ElementId);
    oDiv.style.border = Border;
  }
</script>
</head>

<body bgcolor="#FFFFFF">
<input type="button" value="Show Border" onclick="showBorder('divBorder');"><input type="text" size="30" id="txtShowBorder"><br><br><br><br>
<input type="button" value="Set Border" onclick="setBorder('divBorder', txtSetBorder.value);"><input type="text" size="30" id="txtSetBorder" value="#dd0000 6px dotted"><br><br>
<div id="divBorder" style="position:relative; border: 3px inset #c0c0c0; height:200px;">Hello there</div>
</body>
</html>
--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript Style Switcher Tip rrobbins BOOK: Professional CSS: Cascading Style Sheets for Web Design 0 November 15th, 2007 11:33 AM
Changing tab control style at runtime. John Deutsch Access VBA 2 October 19th, 2007 12:01 PM
problem with changing style when user is logged hertendreef ASP.NET 2.0 Professional 0 February 28th, 2007 06:21 PM
changing xml document object with javascript stekker XML 3 May 7th, 2006 08:29 AM
Changing dialog style from child to popup chrisk10 Visual C++ 0 February 11th, 2005 12:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.