Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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> 
share|improve this question
1  
using Jquery $("#nn").prop('disabled', true); –  Cybermaxs - Betclic May 31 '13 at 7:53
add comment

3 Answers

up vote 1 down vote accepted

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');
}
share|improve this answer
    
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. –  Ant P May 31 '13 at 7:59
    
@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. –  Andrei May 31 '13 at 8:03
    
thx it's works perfectly! –  Pizza Man May 31 '13 at 8:08
add comment
document.getElementById("nn").disabled = true;
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.