Reputation: 381
this is html code
<div id="domTxtEntry">
<strong>Your Name:</strong>
<input type="text" name="txtdisplay" id="txtdisplay" value="" class="input" />
<br />
<br />
<div id="domPrv"></div>
</div>
this jquery code
$(document).ready(function () {
var nameNode = $('#domPrv'),
name = $('#txtdisplay');
//comment...easy
$('#domTxtEntry input').bind('blur keyup', function () {
//name
if ($('#txtdisplay').val()) {
nameNode.text(name.val() + ' modifies this');
}
});
});
this is here in this fiddle. http://jsfiddle.net/qSq37/1/
two problems
Help needed.
Upvotes: 0
Views: 60
Reputation: 1138
The first problem is that when the input is empty, calling .val()
returns false, so the value in #txtdisplay
is not updated. Try this:
$('#domTxtEntry input').bind('blur keyup', function () {
if ($('#txtdisplay').val()) {
nameNode.text(name.val() + ' modifies this');
} else {
nameNode.text("Default value");
}
});
The second problem you can solve like this (removed the <br />
tags):
<div id="domTxtEntry">
<strong>Your Name:</strong>
<input type="text" name="txtdisplay" id="txtdisplay" value="" class="input" />
<div id="domPrv">Default value</div>
</div>
And in CSS:
#domPrv {
display: inline-block;
}
Upvotes: 3