Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set readonly property to false for an HTML text input on clicking an anchor tag

My HTML:

<div class="profileForm">
    <fieldset>
    <label>Name<input type="text" id="name" name="name" runat="server" readonly=""/></label>
    <label>Email<input type="email" id="email" name="email" runat="server" readonly=""/></label>
    <label>Date Of Birth<input type="date" id="dob" name="dob" runat="server" readonly=""/></label>
    <label>Address<input type="text" id="address" name="address" runat="server" readonly=""/></label>
    <label>City<input type="text" id="city" name="city" runat="server" readonly=""/></label>
    <label>State<input type="text" id="state" name="state" runat="server" readonly=""/></label>
    <label>Country<input type="text" id="country" name="country" runat="server" readonly=""/></label>
    <label>Access Level<input type="text" id="accessLevel" name="accessLevel" runat="server" readonly=""/></label>
    </fieldset>
</div>
<div class="profileEdit">
    <fieldset>
        <label><a href="#" id="Aname">edit</a></label>
        <label><a href="#" id="Aemail">edit</a></label>
        <label><a href="#" id="Adob">edit</a></label>
        <label><a href="#" id="Aaddress">edit</a></label>
        <label><a href="#" id="Acity">edit</a></label>
        <label><a href="#" id="Astate">edit</a></label>
        <label><a href="#" id="Acountry">edit</a></label>
    </fieldset>
</div>

My JavaScript

<script src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        console.log("document ready")
        $("profileEdit label a").click(
        function (e) {
            if (this.attr("id") == "Aname") {
                $("#name").attr("readonly", false);
            }
        });
    });
</script>

Alternate JavaScript

<script type="text/javascript">
    $(document).ready(function () {
        console.log("document ready")
        $('#Aname').live('click', function () {
            $("#name").attr("readonly", false);
        });
    });
</script>

What I am trying to do is set readonly attribute of the corresponding input text field to false on click of the corresponding anchor field. None of my JavaScript scripts works.

Solution: after combining @KaraokeStu, @bipin answers I am using asp.net 4.5

$(document).ready(function () {
        console.log("document ready")
        $('.profileEdit label a').live('click', function () {
            alert("ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length));
            $("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).prop('readonly', false);
            console.log($("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).attr('readonly'))
            $("#" + "ctl00_ContentPlaceHolder1_" + this.id.substring(1, this.id.length)).focus();
            alert("done");
       });

    });
like image 336
thunderbird Avatar asked Apr 30 '13 11:04

thunderbird


People also ask

How do you make a readonly false in HTML?

The readonly attribute is a boolean. You can't set it to true or false , you can set it to readonly or not set it at all ( readonly="" is wrong, you can leave the value off ( readonly ) or specify the correct value ( readonly="readonly" )).

How do you set a readonly property in HTML?

The readonly attribute is a boolean attribute. When present, it specifies that an input field or textarea is read-only. A read-only field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).


1 Answers

change the readonly property of an element..use prop()

 $("#name").prop('readonly', false);

link to read more about prop() and attr()

like image 165
bipen Avatar answered Oct 11 '22 17:10

bipen