0
<script type="text/javascript">

$(document).ready(function () {
 $("#title").change(function () {

        var abc = $(this).val();//previously i mentioned this in "" by mistake please update it.
        alert(abc);

    });

});
</script>


Datepicker is working fine for me and change function also works fine at JSfiddle

but dont know what went wrong with it in my application.

Update:
i think jquery not able to recognize "title" as valid id
Proff: i used onchange javascript function on my DropDownListFor like below

 @Html.DropDownListFor(model => model.title, new[] {
 new SelectListItem { Text = blueddPES.Resources.PES.Resource.mr,Value = "Mr" },
 new SelectListItem { Text =blueddPES.Resources.PES.Resource.mrs,Value = "Mrs"},
 new SelectListItem { Text = blueddPES.Resources.PES.Resource.miss,Value = "Miss"},
 new SelectListItem { Text =blueddPES.Resources.PES.Resource.other,Value = "Other"}
 }, "select", new { onChange = "copyText()" })



<script type="text/javascript">
   function copyText() {
    var vall = document.getElementById("title").value;
    alert(vall);
    }
</script>


Now when dropdownvalue changes then an alert box comes with undefined text;

0

4 Answers 4

2

Replace this line

$("this").val();

to this

$(this).val();
Sign up to request clarification or add additional context in comments.

Comments

1

If you cannot find your element with its Id, you may also try to use

 <select onchange="alert(this.value);">
 ...
 </select>

or

 <select onchange="copyText(this);">
 ...
 </select>


<script type="text/javascript">
   function copyText(that) {

    }
</script>

2 Comments

yup, a smart way to do. Do you have any idea why javascript/jquery not recognizing selected id as valid id.?
Either element has no Id, or its Id is not "title". Please try to check its Id at runtime using firebug or some other tool.
0

just use live if elements are created after the page is loaded.

$("#title").live('change', function() {

        var abc = $(this).val();
        alert(abc);

    });

If you are using JQUERY 1.7 or above then use .on()

2 Comments

live() function should be used if the DOM structure is changed while loading the control coz the event binding is lost next time the html renders.
In fact .live() is deprecated and should not be used at all.
0

You have to use

$("this").val()

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.