Remember to insert jQuery into the page and run the script only after you have already loaded the HTML. Because the code itself works perfectly - see at the end of the answer the executable example.
You said in the question that you are using ASP.NET, so the problem should be with the order of things. Note that the script is adding a Listener for the event keyup of an element of your page and, for it to work properly, it is necessary that this Listener be bound after the HTML element has been created. Because it makes no sense to add an event to a non-existent element, right?
Almost always, in ASP.NET, the scripts are defined within a section that is at the bottom of the page, however, these scripts, are usually "rendered" by view engine before HTML - this is defined on the "master" page of layout.
An interesting way to circumvent this unwanted effect is to put the code inside $(document).ready()
, to make sure that the attempt to register the event will only be made after the element has already been created. Something like:
$(document).ready(function() {
$("#num").keyup(function (event) {
// Seu código
});
});
Another possible problem, also related to the "order of things", is whether the element was created dynamically, via Javascript.
In this case, it is necessary to "listen" to an body, as target (destination) the element. For example:
$('body').on('keyup', '#num', function (event) {
// Seu código
});
Executable example of your code:
$("#num").keyup(function (event) {
if (event.which != 13)
return;
var elm = $(this);
var lines = elm.val().split("\n");
for (var i = 0; i < lines.length; i++) {
lines[i] = lines[i].replace(/(\d+\.\s|^)/, (i + 1) + ". ");
}
elm.val(lines.join("\n"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="num" rows="5" cols="32">1. </textarea>
Are you loading jQuery to the page? And this Javascript is after HTML?
– Sergio
Yes, Jquery is loaded on the page and is after HTML
– Leandro Batista
Some error in the console?
– Sergio
No, no error on console.
– Leandro Batista
Do you understand why the answer code worked and your no?
– Sergio
Yes I noticed, there is only one small error to do with a lack of ")" and a ";", but now another problem has arisen, my javascript does not work if the Textarea has the tag runatserver="true", I need this tag in the box.
– Leandro Batista
Where was this error in the question code? The code is with valid syntax and I see no error...
– Sergio
@LINQ aside from that the answer does not solve any specific problem, no :) Ie: is a response with good suggestions, valid code, but it seems to me that tries to hit the problem and not knowing what the problem is.
– Sergio
At the end he has "}", he had to put "});"
– Leandro Batista
But now I have a bigger problem, javascript does not recognize Textarea if it has a runatserver tag
– Leandro Batista
Leandro: "At the end he has "}", he had to put "});" - who is "he"? your code or answer?
– Sergio
@Sergio I actually knew because I know how the order of things works in ASP.NET. Of course this is not specified in the question, but everything suggests that the AP did not change this natural order of things. So, it is not just a kick.
– Jéf Bueno
@LINQ would be good to mention this in the answer. Because AP may not know this...
– Sergio
@Sergio If you see in my answer I refer to this when I speak precisely in "enroll the event only after the element has been created". I didn’t specifically mention ASP.NET because it doesn’t fit, it works like that for anything. Of course the question is confusing and tends to cause problems, but it was, in a way, "responsive". For all intents and purposes, I have already excluded my answer.
– Jéf Bueno
@LINQ I also think it’s "responsive". I tried to figure out if the AP understands the problem and what solves the problem. As that was not the case, and the PA accepted an unaccounted for answer, I thought it best to close. If you think (think) that I acted badly, you can comment and/or vote to reopen. I think the accepted question/answer set is not very useful and I hope that the question/answer will still improve.
– Sergio
Yes, the situation has worsened with the comments yet. I think it is the case to leave closed anyway. I only mentioned it to say that my answer was not a kick to the naked.
– Jéf Bueno