Solved

Javascript can't clear a readonly field

Posted on 2012-03-18
11
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

738 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