7

I have a @Html.DropDownList which calls a jquery function. But it failed to call the function. I have tried-

$(document).ready(function () {
    $('.test').change(function () {
        alert("1");            
    });
});

@Html.DropDownList("PropertyContent", ViewBag.PropertyList as List<SelectListItem>, new { @class="test"})

and

function test() {
        alert("1");
    }

@Html.DropDownList("PropertyContent", ViewBag.PropertyList as List<SelectListItem>, new { onchange="test();"})


@Scripts.Render("~/bundles/jqueryval") is also included in the view

Whats wrong here?

5
  • What's the error? Do you see any error in console? Are you include jQuery in your page? Commented May 15, 2013 at 9:35
  • Also you must write $('.test') instead of $('test'). Because you want to access elements with test class. Commented May 15, 2013 at 9:37
  • @Mojtaba- I have updated my question. Actually I tried with $('.test'). I am not getting any error. Its not invoking the function! Commented May 15, 2013 at 9:41
  • Confirm that jQuery actually shows up in view source - if you have a folder physically named the same as your bundle path, bundling won't work. Commented May 15, 2013 at 11:17
  • Please read about jQuery selectors Commented May 15, 2013 at 11:46

6 Answers 6

4

Your jQuery selector is wrong, use the id-selector instead:

$(document).ready(function () {
    $('#PropertyContent').change(function () {
        alert("1");            
    });
});

A few more possible errors:

  • jQuery is not referenced in the page. To check this, open firebug, chrome dev tools or IE dev tools and check whether you have any errors
  • A common error is to include the $(document).ready inside the view, and only reference jQuery at the bottom. This will generate an error because at that point $ is not known. Possible solutions:
    1. Move the code to an external file and reference that file after the jQuery inclusion
    2. Move your jQuery declaration to the head-section (not recommended as it will slow down the page load)
Sign up to request clarification or add additional context in comments.

10 Comments

no, its my fault when I copied to stack overflow, actually . is there.
Do you see any errors in the console log? Are you including jQuery before the call to $(document).ready ?
I am not getting any error in console and I already included @Scripts.Render("~/bundles/jqueryval")
Check the edit to my answer. If you're including the script inside the view, it's probably executed before jQuery is loaded. You either need to move it after jQuery or move jQuery to the head so it's loaded before your script
Could you post the result HTML (relevant parts) so I can have a closer look at what's going on?
|
3
$(document).ready(function () {
    $('.test').change(function () {
        alert("1");            
    });
});

$('.test').change(function () { // on this part u need to add '.' in test

u forgot that it is a class.

1 Comment

I have updated my question. actually I tried with $('.test')
1

You can also use following code.

$(document).ready(function () {
    $(".test").live("change",function () {
        alert("1");            
    });
});

Comments

0

Working FIDDLE Demo

$(function () {
    $('#PropertyContent').change(function () {
        alert("1");
    });
});

4 Comments

Would you write the generated code for your @Html.DropDownList?
The @Html.DropDownList syntax will generate some HTML for you. I want that HTML to debug the problem.
This is the generated code of @Html.DropDownList- <select name="PropertyContent" id="PropertyContent"><option value="442071">MBS</option> <option value="44050">MM</option> </select>
So you haven't class for it at all. Use the id instead. I've updated my answer.
0

Add the following line somewhere in the view or the layout:

@Scripts.Render("~/bundles/jquery")

Also your selector is not correct. You have no HTML element with class="test".

Try $('#PropertyContent') instead.

Comments

0

Please try to get the dropDownList by its Id like the code below. I got a working example for you: http://jsfiddle.net/pzzQu/

$(document).ready(function () {
    $('#PropertyContent').change(function(){
        alert('1');
    });
});

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.