3

I'd like to add an ellipsis to an input element's placeholder attribute with jQuery prop() but instead of the intended I get literal string ….

How can I add html entities by name with jQuery prop()?

$(document).ready(function(){
 $('div#b input').prop('placeholder','Search…');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div id="a"><input type="text" placeholder="Search&hellip;" /></div><br />
<div id="b"><input type="text" /></div>

3 Answers3

3

The simplest solution is to use the raw hex code instead of the named entity, as these are handled just fine by JS and the DOM API.

$(document).ready(function(){
 $('div#b input').prop('placeholder','Search\u2026'); // <- \u2026 is &hellip;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div id="a"><input type="text" placeholder="Search&hellip;" /></div><br />
<div id="b"><input type="text" /></div>
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
1

You have to invoke the HTML parser one way or the other. You could create a new element, set its HTML content and get its content as text:

$('div#b input').prop(
  'placeholder',
  $('<div />').html('Search&hellip;').text()
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="b">
  <input />
</div>

See HTML Entity Decode

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Interesting approach... a bit much but actually does exactly what I asked more so than the accepted answer since it allows me to use html entities. Thanks. – But those new buttons though.. Jun 04 '15 at 02:41
  • Yeah, I wouldn't do that. Or at least I would have a helper function that only uses a single element for that, not creating one every time I need the conversion. But yeah, just literally answering the question ;) – Felix Kling Jun 04 '15 at 03:08
-1

Just type a horizontal ellipsis:

$(document).ready(function(){
 $('div#b input').prop('placeholder',$(document.createElement('div')).html('Search&hellip;').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div id="a"><input type="text" placeholder="Search&hellip;" /></div><br />
<div id="b"><input type="text" /></div>
Robert McKee
  • 21,305
  • 1
  • 43
  • 57