ZoharYosef
ZoharYosef

Reputation: 257

How to get value of "dynamically id" in jQuery?

How to get value of "dynamically id" in jQuery?

For example: Razor code:

@model ...
...
@{
   int i = 0;
   foreach (var item in Model)
   {
      i = i + 1;
      <tr>
         <td class="check">
            <div tabindex="0" id="a[@i]">print</div>    
      </td>
   </tr>
   }
}

<script type="text/javascript">
    $("#a[1]").keyup(function (e) {
        if (e.keyCode === 13) { // enter == 13
            $("#a[2]").focus();
        }
    });
</script>

"#a[1]","#a[2]" - not work, what can I do?

Upvotes: 1

Views: 2325

Answers (1)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Like other said, put common class for all shared elements or just binding it with this :

// bind only id with a in first words
// but this will problem if exist other element use `a` in front of
$("div[id^=a]").keyup(function (e) {
    if (e.keyCode === 13) { // enter == 13
        hideListShowImg();
    }
});

Updated

$("#a[1]") /* should be ----> */ $("#a\\[1\\]")

Upvotes: 1

Related Questions