tags:

views:

93

answers:

3
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Javascript._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<script language="javascript" type="text/javascript">
    function ClearValue() {

            var txtName = document.getElementById('<%=txtName.ClientID %>');
            txtName.value = hidden.value
                txtName.className = ''
                txtName.className = 'TextBox2'

            }
        }
    </script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title></title>
    <style type="text/css">
  .TextBox
{
    width: 150px;
    border: Solid 1px MistyRose;
    font-family: Verdana;
    font-style: normal;
    color: #333333;
    text-decoration: none;
    font-size: 0.8em;
}

 .TextBox2
{
    width: 300px;
    border: Solid 6px MistyRose;
    font-family: Verdana;
    font-style: normal;
    color: #000000;
    text-decoration: none;
    font-size: 0.3em;
}
  </style>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:ClearValue()" />
        <asp:TextBox ID="txtName" runat="server" CssClass="TextBox"></asp:TextBox>
        <br />
    </div>
    </form>
</body>
</html>

here am trying to change the Css for the text box using javascript.

which is not happening any idea how to solve this issue

thanks

+2  A: 

Your script probably stops because there is no hidden value anywhere.

You also have an extra } at the end of the script.

This should work:

function ClearValue() {
   var txtName = document.getElementById('<%=txtName.ClientID %>');
   txtName.className = '';
   txtName.className = 'TextBox2';
}

Thought this will also work well in modern browsers (though broken in older versions of IE, thanks @David Dorward):

function ClearValue() {
   var txtName = document.getElementById('<%=txtName.ClientID %>');
   txtName.setAttribute('class', '');
   txtName.setAttribute('class', 'TextBox2');
}

You really should try using jQuery which makes this kind of thing a doddle:

function ClearValue()
{
   $('<%=txtName.ClientID %>').toggleClass('TextBox2');
}
Oded
Using `setAttribute` will not work well. It is broken in older versions of IE.
David Dorward
@David Dorward - thanks for the correction. Answer updated.
Oded
Can the downvote please explain?
Oded
@oded: jQuery=bad, especially when having to debug one of the many current bugs. Then your version is automatically different from the hosted version. Then when they make a new feature and don't patch your previous issue...horribad. As you said the problem is probably because of the extra `}` or `hidden.value`. The lack of `;` also could be causing problems.
vol7ron
@vol7ron - Are you suggesting that jQuery is badly broken and should never be used? Can you back this up?
Oded
I'm saying that if you do anything advanced, you'll come across bugs... many bugs - recently, I was doing something simple with a JS slideshow that worked in everything except IE8. The only solution I had was to fix it myself and the moment you have to go into the code and fix it, it takes away the major advantage of the package, which, in my opinion is the caching. -- While everyone is pushing jQuery, the real solution, IMHO, is to further the development of ECMAScript itself. Having something with less overhead, native to browsers, that takes less code is the best hope.
vol7ron
While I say this, the simplicities of the language don't have problems, but why use it if you're doing something simple? it may be likely that I adopt `jQuery` in the future; but as for now, I'm still waiting for it to evolve just a little bit more. Until that happens, I'll probably remain re-inventing the wheel, which I'm not ashamed of, as I rather know what it takes to do/make/modify something, than be handed it w/o knowing the intricacies.
vol7ron
A: 

try with

txtName.setAttribute("class",'TextBox2);
frisco
The `className` property (used in the original question) is better supported than `setAttribute`
David Dorward
A: 
             txtname.setAttribute("className", newClass); //For IE; harmless to other browsers.

From http://www.webdeveloper.com/forum/showthread.php?t=134282

Hulk
Using the `className` property (as the original question already does) instead of the `setAttribute` method avoids the need to hack older versions of IE in this way.
David Dorward