ASP.NET My Javascript is not working

Asked

Viewed 218 times

-1

Hello, I’m using Javascript to create a numbering in a Textarea when the Enter key is pressed, an example :

1. Olá
2. Isto é isto
3. Fim

This Javascript, however, is not working and I do not know why, here is Javascript:

<script type="text/javascript">
  $("#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>

And this is the Textarea code:

<textarea id="num" rows="5" cols="32">1. </textarea>

It’s not working and I’ve tried several things, I know the code works, though, because I went to an online compiler and it was running perfectly.

  • Are you loading jQuery to the page? And this Javascript is after HTML?

  • Yes, Jquery is loaded on the page and is after HTML

  • Some error in the console?

  • No, no error on console.

  • Do you understand why the answer code worked and your no?

  • 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.

  • Where was this error in the question code? The code is with valid syntax and I see no error...

  • @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.

  • At the end he has "}", he had to put "});"

  • But now I have a bigger problem, javascript does not recognize Textarea if it has a runatserver tag

  • Leandro: "At the end he has "}", he had to put "});" - who is "he"? your code or answer?

  • @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.

  • @LINQ would be good to mention this in the answer. Because AP may not know this...

  • @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.

  • @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.

  • 1

    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.

Show 11 more comments

2 answers

5

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>

0


See the most well explained @LINQ response.

tries to put in like this

$(document).ready(function () {
$("#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"));
    });
});
  • Can you explain why this code solves the AP problem? In particular by relating to the comment "jQuery is after HTML"

  • My intention was to speak only to place within a $(Ocument). ready(Function () { }); the code that could haphazardly work I’m not metre on stackoverflow and I’m also in the middle of work

  • Bruno, and apparently you solved the problem :) I just wanted to make it clear what was the difference because it solves the problem.

  • Well I would also like and the answer of the full penalty @LINQ that deleted. could help someone in the future.

  • I agree :) And I was surprised that your answer (which seems useful to the AP) was accepted and his not. So I got the feeling that the AP doesn’t understand the problem it has, nor its solution.

  • Thank you @LINQ you understand what you’re talking about. I’m starting in this world of coffee addiction.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.