Jquery version of this old bit of JS

What would be the JQuery version of this old bit of JS?

<html>
	<head>

<script>
var http =  new XMLHttpRequest();

function send(action,htmltarget) {
   http.open('get', action);
   theTarget = htmltarget
   http.onreadystatechange = handleResponse;
   http.send(null);
}

function handleResponse() {
 if(http.readyState == 4 && http.status == 200){
       var response = http.responseText;
           document.getElementById(theTarget).innerHTML = response;
   }
}
</script>
	</head>
	<body>
		<a href="" onclick="send('response.php','target1');return false;">Click me</a>
		<div id="target1"></div>
	</body>
</html>

Open in new window

Stephen ForlanceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
$('#target1').load('response.php');

Open in new window

0
Jan LouwerensSoftware EngineerCommented:
Here's a jQuery replacement for the entire file:

<html>
   <head>
<script>
$(function(){
   $('#source1').on('click', function(event){
      event.preventDefault();
      $('#target1').load('response.php');
   });
});
</script>
   </head>
   <body>
      <a id="source1">Click me</a>
      <div id="target1"></div>
   </body>
</html>

Open in new window

0
Stephen ForlanceAuthor Commented:
I tried the following, but got an error about $ not being defined:

<html>
   <head>
<script>
$(function(){
   $('#source1').on('click', function(event){
      event.preventDefault();
      $('#target1').load('response.php');
   });
});
</script>
   </head>
   <body>
      <a id="source1">Click me</a>
      <div id="target1"></div>
   </body>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</html>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

leakim971PluritechnicianCommented:
put line 16 before line 3
0
Jan LouwerensSoftware EngineerCommented:
You'll need to import the jQuery library. (I assumed you were already doing this, since you're asking for jQuery code.

<html>
	<head>
      <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(function(){
   $('#source1').on('click', function(event){
      event.preventDefault();
      $('#target1').load('response.php');
   });
});
</script>
	</head>
	<body>
      <a id="source1">Click me</a>
      <div id="target1"></div>
   </body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stephen ForlanceAuthor Commented:
Thank you!
On other sites its says the JQuery should be included at the end of the page, once the whole page has loaded ,is this not the case?
0
Jan LouwerensSoftware EngineerCommented:
No, not the library import. (You can't use any jQuery code until after the library is loaded. Up top, in the head, would be the best place for it.)

Perhaps the other sites meant to suggest that the bit of jQuery code they were talking about should be at the end of the page, not the library import, itself.
0
leakim971PluritechnicianCommented:
if you want to put the jquery lib bottom, put your script bottom too after it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.