Solved

Javascript can't clear a readonly field

Posted on 2012-03-18
11
467 Views
Last Modified: 2012-03-19
I have a javascript and an asp.net textbox. If I have the textbox field to readonly, then javascript below won't set the value to blank. If i remove "readonly", then it works. It makes sense since it's readonly (i can set it in aspx code behind to blank even if it's readonly but not in javascript)...

Is there a way around this to make the textbox readonly and still clear the field with javascript?


 
<asp:TextBox ID="lblSpecialty" BorderStyle="None"  style="font-size:11px;  color:#ff0000;line-height:15px;padding:0 0 0 10px;display:block;" BackColor="#eff5fb"  runat="server" Text="" ></asp:TextBox>
        

<script type="text/javascript">
    function clrCtrl() {

        document.getElementById("<%= lblSpecialty.ClientID %>").value = "";
       
    } 

</script>

Open in new window

0
Comment
Question by:Camillia
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37736085
run fine for me...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37736090
in both cases (readonly or not)

my test page :
<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
    function clrCtrl() {
        document.getElementById("<%= lblSpecialty.ClientID %>").value = "AAAAAAAAAAAAAA";
    }
    window.onload = clrCtrl;
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="lblSpecialty" BorderStyle="None"  style="font-size:11px;  color:#ff0000;line-height:15px;padding:0 0 0 10px;display:block;" BackColor="#eff5fb"  runat="server" Text="tytytyyt" ReadOnly="true" ></asp:TextBox>
</asp:Content>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 37736195
i have a master page but doubt that's the case. How can debug it?? how can make that field read-only in Javascript if I cant do it in aspx (.net)?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 14

Expert Comment

by:nishant joshi
ID: 37736273
disable the javascript and just check in browser what is the the of lblSpecialty is it no change then just put harcoded value.
0
 
LVL 5

Expert Comment

by:Vishal Kedar
ID: 37736583
use below code

<script type="text/javascript">
    function clrCtrl() {

        document.getElementById("<%= lblSpecialty.ClientID %>").innerText = "";
      // or
       document.getElementById("<%= lblSpecialty.ClientID %>").innerHTML = "";
       
    }

</script
0
 
LVL 7

Author Comment

by:Camillia
ID: 37737237
let me try
0
 
LVL 13

Accepted Solution

by:
Rick earned 500 total points
ID: 37739897
@ 37736090:

It worked for me too (readonly or not).


Farzadw:

If it works when you remove the readonly property, then try this:


<script type="text/javascript">

        function clrCtrl() {

            var txt = document.getElementById("<%= lblSpecialty.ClientID %>");

            txt.readOnly = false;
            txt.value = "";
            txt.readOnly = true;

        }

</script>
0
 
LVL 7

Author Comment

by:Camillia
ID: 37739984
let me try
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37740029
0
 
LVL 7

Author Comment

by:Camillia
ID: 37740158
hmm, i'm in a user control not an aspx page. I wonder if that would make a difference...would it?? i tried all of the solutions. This one works: ID: 37739897
but the lblspecialty text-value shows, it's still NOT readonly. User can actually delete that msg. Let me see if i can make it read only in the code and see if Javascript can clear it...
0
 
LVL 7

Author Comment

by:Camillia
ID: 37740331
I needed to do this :OnClientClick="clrCtrl();return false;" to make solution
Posted on 2012-03-19 at 21:50:07ID: 37739897 work. Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question