Here is the html
<body style="background-color:#999;font-size:12px;">
<div style="width:500px; height:500px; margin:30px auto; background-color:#9CC">
<input id="Test" type='text' placeholder='Hello how are you' style="width:270px;" / >
</div>
</body>
I want to know the text width only. Not the input width nor the number of character. Please help me with a proper suggestion.
Here is the html
<body style="background-color:#999;font-size:12px;">
<div style="width:500px; height:500px; margin:30px auto; background-color:#9CC">
<input id="Test" type='text' placeholder='Hello how are you' style="width:270px;" / >
</div>
</body>
I want to know the text width only. Not the input width nor the number of character. Please help me with a proper suggestion.
Share Improve this question edited Jun 6, 2014 at 11:37 Subhajit Panja asked Jun 6, 2014 at 11:24 Subhajit PanjaSubhajit Panja 1,2401 gold badge9 silver badges22 bronze badges 6- for width text, you mean at the length or pixels? – napster3world Commented Jun 6, 2014 at 11:27
- he want the width taken by placeholder text. – Amit Kumar Commented Jun 6, 2014 at 11:30
- ok, but number of chars ex.(17 characters) or width ex. (100px)? – napster3world Commented Jun 6, 2014 at 11:31
-
@napster3world
How to know Placeholder String width in px
– A. Wolff Commented Jun 6, 2014 at 11:33 - 2 +1 for a great question – codefreaK Commented Jun 6, 2014 at 11:40
1 Answer
Reset to default 10You could use following snippet:
var _$tmpSpan = $('<span/>').html($('#Test').attr('placeholder')).css({
position: 'absolute',
left: -9999,
top: -9999
}).appendTo('body'),
textWidth = _$tmpSpan.width();
_$tmpSpan.remove();
alert(textWidth);