Solved

Javascript can't clear a readonly field

Posted on 2012-03-18
11
459 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
Comment Utility
run fine for me...
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
use below code

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

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

</script
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Author Comment

by:Camillia
Comment Utility
let me try
0
 
LVL 13

Accepted Solution

by:
Rick earned 500 total points
Comment Utility
@ 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
Comment Utility
let me try
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hovering effect 9 28
angularls and plnkr 14 16
Problem to page 4 16
Uncaught TypeError: can't read property 1 4
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…

762 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

14 Experts available now in Live!

Get 1:1 Help Now