Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  101] [ 2]  / answers: 1 / hits: 5637  / 11 Years ago, mon, january 20, 2014, 12:00:00

hey guys im trying to change the text of a h1 with jquery. i want to change the text every time enter key is pressed in a text area and the values are numeric values like on first enter the text should be 1 and on other it should be 2 and so on.. This is the code which im using...



Jquery



 <script>
$('#_co').bind('keypress', function(e) { //_co is the Id of Text area
var code = e.keyCode || e.which;
var id = 1;
if(code == 13) {
var text = $('#_line').text();
$('#_line').text(id);
id++;
}

});
</script>


Html



<h1 id=_line>0</h4>

More From » jquery

 Answers
4

Just move your declaration outside of that function,



var id = 1;

$('#_co').bind('keypress', function(e) { //_co is the Id of Text area
var code = e.keyCode || e.which;

if(code == 13) {
var text = $('#_line').text();
$('#_line').text(id);
id++;
}

});


And as Rohit said in the comment, You are having an invalid markup, Just close the h1 tag properly,



<h1 id=_line>0</h1>





As per your new requirement you can use .append() in your current context.



var id = 1;

$('#_co').bind('keypress', function(e) { //_co is the Id of Text area
var code = e.keyCode || e.which;

if(code == 13) {
var text = $('#_line').text();
$('#_line').append('<br>' + id);
id++;
}

});


DEMO


[#48542] Saturday, January 18, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;