Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Unable to read attribute set by Razor through jQuery, why?

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 class="row">
  <label class="col-md-offset-3 col-md-9">
    <input type="radio" class="myclass" 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>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to pass this to myHandler:

@foreach (var item in Model) {
<div class="row">
  <label class="col-md-offset-3 col-md-9">
    <input type="radio" class="myclass" 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>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!