0

I have this code , it works when I put it in the same file, but I must separate the html from the javascript, now my problem is how can the function know the #r so it can put the data in it?

 <button id="charger">GET Data</button>
    <div id="r">Click</div>

<script src="jquery.js"></script>
<script>
  $(function() {
    $('#charger').click(function() {
        $.getJSON('url', function(donnees) {
        $('#r').html('<p><b>Name</b> : ' + donnees.name + '</p>');
        $('#r').append('<p><b>Message</b> : ' + donnees.msg + '</p>');
        $('#r').append('<p><b>id</b> : ' + donnees.Type + '</p>');
      });
    });  
  });
</script>

and my modification so far is : page.html: ...

<p class ="submit"><input  type="submit" id="charger" value="OK" onclick="test();"></p>
    <div id="r">Your data : </div>

and the js file :

function test() {
    $('#charger').click(function() {
       $.getJSON('url', function(donnees) {
            $('#r').html('<p><b>Name</b> : ' + donnees.name + '</p>');
            $('#r').append('<p><b>Message</b> : ' + donnees.msg + '</p>');
            $('#r').append('<p><b>id</b> : ' + donnees.Type + '</p>');
      });
    });  
  }

in the console , the function works , but it doesn't show me the data ..

1 Answer 1

1

It's because the only thing your test function does is adding an event listener to the click event of the #charger input. Just take your initial code as is and put it in the seperate js file and that should do the trick. Also, make sure that the jQuery script get's loaded before your other script.

Sign up to request clarification or add additional context in comments.

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.