console.log(myvar)
shows undefined
when it shouldn't be. The last console.log shows
exactly the full tag, where I can see that data-mydata
has a value. Why is this happening?
@foreach (var item in Model) {
<div >
<label >
<input type="radio" id="@item.Id" name="myname" onchange="myHandler()" value="@item.property" data-mydata="@Html.Encode(item.property2)"/>
@item.Name
</label>
</div>
}
<script>
function myHandler() {
var myvar = $(this).data('mydata');
console.log(myvar);
console.log($("input[name='myName']:checked")[0]);
}
</script>
CodePudding user response:
Try to pass this
to myHandler
:
@foreach (var item in Model) {
<div >
<label >
<input type="radio" id="@item.Id" name="myname" onchange="myHandler(this)" value="@item.property" data-mydata="@Html.Encode(item.property2)"/>
@item.Name
</label>
</div>
}
<script>
function myHandler(t) {
var myvar = $(t).data('mydata');
console.log(myvar);
console.log($("input[name='myName']:checked")[0]);
}
</script>