Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange output when trying to get an element's HTML code value?

I have a <span> element with the following text:

List<Tuple<string, string>>

When I get its html attribute using jQuery, I get a strange outcome:

List<tuple<string, string="">&gt;</tuple<string,>

I realize it's parsing the value as HTML, but using .text() also returns an incorrect value. Can anyone explain why this is happening and how can I resolve it?

Here's a snippet reproducing the issue:

console.log($('#lbl').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="lbl">List<Tuple<string, string>></span>
like image 691
Koby Douek Avatar asked Mar 02 '26 00:03

Koby Douek


2 Answers

Why this is happening: because your HTML is invalid. You should not use the < or > characters except for delimiting tags. Instead you must use &lt; and &gt; respectively. Your browser tries to be robust to this kind of error, and so it tries to make sense of those strange looking tags. The result you get is the best it could do about it (but another browser may interpret it differently, as this is not standard).

How can you resolve it: write it as correct HTML, that is:

<span id="lbl">List&lt;Tuple&lt;string, string&gt;&gt;</span>
like image 65
Pierre-Antoine Avatar answered Mar 04 '26 18:03

Pierre-Antoine


You need to replace < with '&lt;' and > with '&gt;' to show in html.

console.log($('#lbl').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="lbl">List&lt;Tuple&lt;string, string&gt;&gt;</span>
like image 32
Piyush Bhati Avatar answered Mar 04 '26 18:03

Piyush Bhati