Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change DisplayFor Text/Value through jquery

I need this displayfor to update its value from using jquery.

I tried .val() and .text() but neither worked, is there a special way to do this for Displays, I need it to be and stay as a Display.

Also can the style not be set for Displays? I never could get that to work, it works for TextboxFor.

Thanks for any tips.

Estimated Time: @Html.DisplayFor(model => model.EstimatedTime, new { @class = "TitleEstTimeClass", @style = "color: green" })

$(".TitleEstTimeClass").val(result[1]);
like image 546
TMan Avatar asked Apr 29 '12 17:04

TMan


1 Answers

DisplayFor generates a label HTML tag <label id="myId" class="myClass">Some Text</label>. (INCORRECT)

EDIT

I was wrong and thought you were using the LabelFor helper. The DisplayFor helper outputs no HTML markup (like a label or span tag). If you want this to work with the DisplayFor helper, you will need to wrap the DisplayFor tag with a <span> or something similar like below:

<span class="TitleEstTimeClass" style="color: green">
   @Html.DisplayFor(model => model.EstimatedTime)
</span>

Given this, try using the .html() JQuery property like below - it really is no different than updating the text in div or span:

$(".TitleEstTimeClass").html(result[1]);

Also, be sure that your JQuery call is in the appropriate <script></script> tags and that the function that you are calling is actually being fired. I am not sure if the above code is straight from your project or just snippets, but as written above, nothing will happen.

like image 77
Tommy Avatar answered Nov 17 '22 08:11

Tommy