0

I have some values in my viewbag.I access that viewbag contents using Jquery.Initially my controllers are disabled.But according to viewbag rules i need to enable some some components.

Problem is thisone not enabled according to viewbag values.always show it disabled mode.

<script type="text/javascript">
$(document).ready(function () {
    $('.txtnews').attr("disabled", "disabled") //Initially disabled
    $('.txthq').attr("disabled", "disabled")

    if ('@ViewBag.NewsMode'.indexOf("Show")) { // accroding to viewbag values,need thisone enabled.
        $('.txtnews').attr("enabled", "enabled")
    }
    if ('@ViewBag.NewsMode'.indexOf("hqm")) {
        $('.txthq').attr("enabled", "enabled")
    }

})
 </script>
1
  • you can try $('.txtnews').attr('disabled', false); Commented Jul 15, 2015 at 16:19

1 Answer 1

2

I am not sure enabled is a valid attribute, perhaps you just want to remove the disabled attribute instead? For this you can use removeAttr()

For example:

if ('@ViewBag.NewsMode'.indexOf("Show") != -1) {
    $('.txtnews').removeAttr("disabled");
}

Here is a working example


Alternatively you could tidy up your logic and only apply disabled when needed. For example:

$(document).ready(function () {
    if ('@ViewBag.NewsMode'.indexOf("Show") == -1) {
        $('.txtnews').attr("disabled", "disabled");
    }
    if ('@ViewBag.NewsMode'.indexOf("hqm") == -1) {
        $('.txthq').attr("disabled", "disabled");
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

@TechGuy: Just a point, if NewsMode starts with "Show" then indexOf will be 0 (which is false). Instead you could do >= 0 or != -1 to check if it contains it

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.