1

I want to disable any editorfor with javascript, I try this but it's not worked :

<body onload="charge()">
    <div class="editor-field">
       <%: Html.EditorFor(model => model.Num_Serie, new { @id = "nn" })%>        
    </div>
</body>

and the javascript code :

<script type="text/javascript">

        function charge() {

            var nn = document.getElementById('nn');

            nn.disabled = 'disabled';

        }

    </script> 
1
  • 1
    using Jquery $("#nn").prop('disabled', true); Commented May 31, 2013 at 7:53

3 Answers 3

2
document.getElementById("nn").disabled = true;
Sign up to request clarification or add additional context in comments.

Comments

1

EditorFor is not rendered into one HTML element, rather it is a collection of inputs. Therefore you should disable all of them:

function charge() {
    var inputs = $('div.editor-field :input');
    inputs.attr('disabled', 'disabled');
}

2 Comments

Precisely what I came here to say - there seems to be a common confusion that a HtmlHelper necessarily equates to a single HTML element, which is not true. Particularly, in fact, with EditorFor, where it is impossible to know what will be rendered without seeing both the model and the project's template structure.
@RabNawaz, true enough. However I assume jquery is there - this is an ASP.NET MVC project, jQuery is included there by default. It is not a problem to rewrite the same without jquery though, but it will require either more details from the OP, or way more javascript code - both will distract from main point.
0

for this particular case, this jQuery code will work. $('#nn').attr('disabled','disabled');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.