-1

I have this td "named dynamically":

@foreach (var m in Model)
{
    <td id="mod @m.codFilial" style="width:auto;">@m.mod</td>
}

As you can see, its ID is named after the value on the foreach, prefixed by "mod ".

However, I can't seem to have access to each td through jquery:

<script>
    $("mod #@m.codFilial").append("<p>Text to append.</p>");
</script>

I mean, the text isn't been appended, probably because it's not recognizing the element. Am I misleading the syntax or some logic feature?

Rui Jarimba
  • 9,732
  • 10
  • 46
  • 74
  • I'd recommend you to remove the space from the ID element. Also, it should be `$("#mod @m.codFilial")` instead of `$("mod #@m.codFilial")` – Rui Jarimba Oct 02 '18 at 18:41
  • Thank you a lot, guys. The problem was really the space between those parts, I wasn't acceptable as a character for name – Valmir Júnior Oct 03 '18 at 12:04

3 Answers3

2

Element ids cannot contain spaces, see e.g. this answer for details.

You could use e.g. an underscore instead of a space:

@foreach (var m in Model)
{
    <td id="mod_@m.codFilial" style="width:auto;">@m.mod</td>
}

Last but not least, in the jQuery selector you need to start with the # character in order to use the entire id.

<script>
    $("#mod_@m.codFilial").append("<p>Text to append.</p>");
</script>
Peter B
  • 18,964
  • 5
  • 26
  • 60
0

Don't use space,@ in selector

<td id="mod @m.codFilial"  

use

<td id="mod_m.codFilial"

because js will identify child of tag 'mod' where have id '@m.codFilial' if you use it

and in js

$("#mod_m.codFilial").append("<p>Text to append.</p>");
0

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

What are valid values for the id attribute in HTML?

Neetin V
  • 13
  • 4