1

I try to load a text file with button. Without button it works. But with button it doesn' t.

I have this:

$("#lesen").click(function () {
jQuery.get('movie.txt', function (data) {

        var lines = data.split("\n");

        $.each(lines, function (n, elem) {
            $('#content_area').append('<div>' + elem + '</div>');
        });

    });
});

and this is the html file:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type ="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="Scripts/JFunc.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>


      <input type="button" id="lessen" value="load"  />
    <div id="content_area"></div>
      </div>

    </form>


</body>
</html>

Thank you

I have it now like this:

$("#lessen").click(function () {
    alert('lsdjflkjsdf');
jQuery.get('movie.txt', function (data) {

        var lines = data.split("\n");

        $.each(lines, function (n, elem) {
            $('#content_area').append('<div>' + elem + '</div>');
        });

    });
});

but I even dont see the alert, when I activate the button

0

3 Answers 3

2

Maybe a typo, your button id is lessen and your jQuery selector refers to #lesen:

$("#lesen").click(function () {

Try this:

$("#lessen").click(function () {

And to make sure the DOM is loaded before the script is executed, you should pass it as a callback to $(document).ready():

$(document).ready(function() {
  $("#lessen").click(function () {
    alert('lsdjflkjsdf');
    jQuery.get('movie.txt', function (data) {
      var lines = data.split("\n");
      $.each(lines, function (n, elem) {
        $('#content_area').append('<div>' + elem + '</div>');
      });
    });
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That was it!!
1

You should write the same id names in both the files at every place you refer.

$("#lesen").click(function () {.....

Replace the above line with the below snippet in your jQuery code

$("#lessen").click(function () {.....

Actually you misspelt the id name #lessen in your JavaScript file @ line 1

Comments

0

use this id = "lessen ".

$("#lessen").click(function () {
  //Your code hare . 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.