Reputation: 4334
I want to display textbox with a % sign next to the textbox but the % does not appear, how do I make it appear?
Below is my code:
var $weightText = $("<input type='text' id='txtWeightRow' maxlength='5' onkeypress='return isNumberKey(event)'/>").attr('name',$this.attr('name'))
.attr('value',$this.val())
Upvotes: 1
Views: 821
Reputation: 434735
From the fine manual:
If a string is passed as the parameter to
$()
, jQuery examines the string to see if it looks like HTML (i.e., it has<tag ... >
somewhere within the string). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements.
Looks like jQuery is trying to figure out what is in that string and not quite getting it right. If you try this (at least in a WebKit browser):
console.log($("<input type='text' id='txtWeightRow'>%"));
console.log($("<input type='text' id='txtWeightRow'>%<br>"));
You'll see that only the <input>
comes out in the first one but you'll get all three for the second. I'd call that a bug in jQuery.
I think you'll have to do it in two steps to make it work:
var $weightText = $("<input type='text' id='txtWeightRow'/>").attr('name', $this.attr('name'))
//...
$(whatever).append($weightText).append('%');
where whatever
does, of course, depend on where you're trying to put the <input>
.
Upvotes: 0
Reputation: 6228
To add a % next to your text box, you'd want to do something like this:
$('#txtWeightRow').after('%');
Upvotes: 2
Reputation: 557
If you want to select the value of another textbox you should do something like this:
var txtValue = $('#wanted-textbox').val();
Upvotes: 0
Reputation: 5264
var $weightText = $("<input type='text' id='txtWeightRow'/>").attr('name',$this.attr('name'));
$("#txtWeightRow").after('%');
Upvotes: 1