Solved

Javascript can't clear a readonly field

Posted on 2012-03-18
11
462 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now