-1

I have a form with form elements such as follows:

@using (Html.BeginForm("Edit", "UserProfile", FormMethod.Post, new { id = "EditUserForm", @class = "form-horizontal form-bordered" }))
{

    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-sm-4 control-label" })
        <div class="col-sm-8">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", type = "text", @readonly = "readonly" })
        </div>
    </div><!-- form-group -->

    <div class="form-group">
        @Html.LabelFor(m => m.Name, new { @class = "col-sm-4 control-label" })
        <div class="col-sm-6">
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control", type = "text", @readonly = "readonly" })
        </div>
    </div><!-- form-group -->

}

I want to use jquery to loop through all the elements within the form and to change / remove the readonly Attribute. When I click a button / element.

how would I go about doing this. I only know how to change the attribute of a single item which Id I know?

html is For Razor view.

2
  • It is much better to provided rendered HTML, if question is only about client side. Commented Sep 30, 2014 at 12:02
  • My code will also work for this question @zapnologica Commented Sep 30, 2014 at 12:11

3 Answers 3

2

You can use:

$(function () {
    $('#EditUserForm [readonly]').prop('readonly', false);
});
Sign up to request clarification or add additional context in comments.

Comments

1

FIDDLE

    $(".form-group input[type=text").each(function(){
        var attr=$(this).attr('readonly');
        if(typeof attr !==typeof undefined || attr!==false)
           $(this).removeAttr('readonly');
    });

4 Comments

typeof attr !==typeof undefined can be simplified into attr !== undefined.
No, it is just unnecessary symbols. !== compares looking at operands type, while != looks only on values, converting them to one type.
yes , since you know this , hope you understand why this is more accurate .And you can't say this is unnecessary symbols as this will help you know the values type also .
attr !== undefined works as typeof attr != typeof undefined || attr != undefined. So, once more, as there is internal checking for type inside !==, there is no need to check types, just attr !== undefined for accurate way for checking for undefined value. Take a look at fiddle for example and a little explanation.
1

Since there is only input fields in your form.

Try Something like this:

$('input[readonly]').removeAttr('readonly');

OR

$('#EditUserForm input[readonly]').removeAttr('readonly');

Working Demo

2 Comments

Yes, it looks good for me now. It's very hard to understand OP's HTML, but I think it's what is required, +1.
@Regent..yes the OP is using Asp.Net MVC server side technology..all..@Html.TextBoxfor() will generate simple textboxes...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.