0

I need some help. I need to know if it is possible to use the % symbol in javascript. I ask this question because I have an html table with the following ID= MRRMFBSY_%_CEC. When I try to keep the TD of the second TR of this table the results is undefined, so it seems that it doesnt find the Table with this ID and also when it is defined well. See my code below:

  function getColumnsVal(id) {

    var header = $("table#" + id + " thead tr:eq(1)");
    var header_fields = $("td", header);
    // If ID = MRRMFBSY_%_CEC when I try to do an alert of one of my TD, 
    // example the firstone it returns undefined
    alert(header_fields[0]);

  }

The question if you think that the problem is the % symbol or not, because when I have the other ID it works perfectly. Thanks in advance

Udo E.
  • 2,665
  • 2
  • 21
  • 33
Dana
  • 19
  • 6
  • 2
    `%` is a modulus operator, so reserved. – Keith Aug 09 '19 at 10:45
  • 1
    No, you cannot use % for variable names, it is JavaScript's modulo operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators – Patrick Hund Aug 09 '19 at 10:46
  • You can use it as an `id` for an element. – adiga Aug 09 '19 at 10:49
  • Thanks a lot to both of you for the fast answer, so just to be sure i have understand. Also if it is the value of the ID (it is not the name of the variable ) i cannot use it because it is a reserver word. is it right= – Dana Aug 09 '19 at 10:51
  • @Dana what does the commented line in your code look like when you uncomment it? Please you can put it in the comment box here. – Udo E. Aug 09 '19 at 10:56
  • Depending on your use case, it's worth pointing out object properties are much more relaxed. But you will have to access the value using bracketed notation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – Keith Aug 09 '19 at 11:00
  • @UdoE. I don't understand your question, you want to see the print of the alert? the print results is: undefined – Dana Aug 09 '19 at 11:09
  • @Dana, no. I want to see the if condition you are using to test for ID. I mean the code where you actually used MRRMFBSY_%_CEC – Udo E. Aug 09 '19 at 11:11
  • 1
    Possible duplicate of [Select element with percent sign (%) in its id with jQuery](https://stackoverflow.com/questions/20715337/select-element-with-percent-sign-in-its-id-with-jquery) – misorude Aug 09 '19 at 11:29
  • @Dana, I now understand your question, there was a problem with your comments, which I've corrected – Udo E. Aug 09 '19 at 11:50

2 Answers2

1

% is a reserved character, since its an operator (see).

It's not recommended, but you can use it as ID in an HTML element.

See this example:

const element = document.getElementById('MRRMFBSY_%_CEC');

console.log(element); // returns the div element
<div id="MRRMFBSY_%_CEC">
  My div with a specific ID
</div>
StefanN
  • 871
  • 6
  • 19
0

ISSUE:

There is a problem when using certain special symbols %, ^, +, #, and so on, inside a jquery selector. They should be escaped with a backslash(\) when used because they are also used in forming the queries for the selector.

For instance '#divid' is a valid string in JavaScript but would be confusing to use in jQuery if the string was an actual id of an element. To get this element you have to use $('#\#divid').

So, in your case to get your target element, $('#MRRMFBSY_\%_CEC') will get the element easily. However, you can either insert the escape character(\) manually or programmatically as done in this post with regular expression. Therefore, using the square brackets or the native getElementById in this answer, is just another way out of this problem.

You can definitely use % symbol in an id attribute (or in any string) as you would use the dash symbol -. However, you cannot use either of both for JavaScript variable names as they are reserved symbols.

SOLUTION:

Though this question has its own intricacies, @misorude has pointed out a solution here. So there lies your answer. use the square brackets [] or document.getElementById like this.

  function getColumnsVal(id) {
    // var element = $('[id="' + id + '"]'); // this line is equivalent to the next line.
    var element = $(document.getElementById(id));
    var header = element.find($("thead tr:eq(1)"));
    var header_fields = $("td", header);
    // If ID = MRRMFBSY_%_CEC when I try to do an alert of one of my TD, 
    // example the firstone it returns undefined
    alert(header_fields[0]);

  }
Udo E.
  • 2,665
  • 2
  • 21
  • 33