Solved

Javascript can't clear a readonly field

Posted on 2012-03-18
11
468 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
selected value 16 24
Json and ajax 1 18
Html date format 9 20
JQuery - Default innerhtml text with keyup function (copy as you type) 3 14
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 …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

685 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