Question

I am using uniformjs form controls which are working except the listmenu. When i add '&' symbol (&) inthe list menu, it renders correctly, but problem is coming when i change the value to different value and select again the value which has & symbol it renders as & instead '&' symbol in the list menu. enter image description here

enter image description here

<select>
<option>Through &amp; Google</option>
<option>Through Twitter</option>
<option>Other&hellip;</option>         
<option>&lt;Hi&gt;</option>
</select>

http://uniformjs.com/#example

can someone tell me what is the problem..

Was it helpful?

Solution

I think the problem might come from this line (source - line 185):

spanTag.text(elem.find(":selected").html());

If you have the following html:

<select>
    <option>One &amp; Two</option>
    <option>One & Two</option>
</select>
  1. The plugin gets the content as html doing elem.find(":selected").html()

    Both option element will return this value when getting html: One &amp; Two Special characters are represented by html entities (&amp;for & in our example)

  2. and then plugin applies this result as text using spanTag.text(<html>);

    So html entities do not get parsed (&amp; is displayed as &amp;)

This fiddle illustrates it.

I don't think there is a solution to that except to not use special characters like &...

OTHER TIPS

According to Didler, why not modify the uniformjs a little bit?

I have met the same scenario recently, and thanks Didler to point out the exact line which saves me heaps of time to locate the place.

In my case, I have a select with value consists of special characters, such as:

<option>aaa</option>
<option><a>bbb<br>ddd<hr>ccc</option>

So that I modify the code in line 185 to:

spanTag.text(elem.find(":selected").text());

which solves the problem that when render the value it is in a correct shape.

In op's scenario, I'm not sure which server side language you are using, but definitely there is a way to escape the text within option before generating the html page so that there is no &amp; in your html but the character itself. I'm using Java so that I can simply use JSTL <c:out value="${******}"/> to put the value in the option tag.

There's a pull request (130) that updates 4 lines of code (lines 173, 185, 212, and 569) so that instead of using .html() they use .text(). The two later pull requests I saw don't seem to update all 4 lines.

If you wish to update the minified version as well, you can search for these two snippets:

  • l.html(n.html() (x1 - change second instance of 'html')
  • :selected").html( (x3 - change only instance of 'html')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top