Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Get Div value when clicked using AJAX
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">

I am trying to get value of div where ID is Class when Div is clicked.

function caller(){
          $.ajax({
              url: "test.php",
              type: "get",
              data: {
                Class_info: $('#Class').val()
              }
          }).done(function(data) {
              $('#Result').text(data);
              alert(data);
        });
      }

Looks like it isn't reading the value of Div when clicked. Am I using AJAX wrongly?

test.php

<?php
  $a = $_GET['Class_info'];
  echo($a);
?>
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

div tag dont have value attribute. So you can use $('#Class').attr('value'); check snippet below..

function caller(){
  alert($('#Class').attr('value'));
  $.ajax({
      url: "test.php",
      type: "get",
      data: {
        Class_info: $('#Class').attr('value')
      }
  }).done(function(data) {
      $('#Result').text(data);
      alert(data);
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "Class" class = "Class_ticket" onclick = "caller()" value = "1">test</div>

over 4 years ago · Santiago Trujillo Report

0

You can use this little trick:

<div id "" class="Class_ticket" onclick="caller()" value="1">
    <input type="hidden" value="1" id="Class" />
</div>

note that the input is hidden!

And here is your ajax:

function caller() {
    $.ajax({
        url: "test.php",
        type: "get",
        data: {
            Class_info: $('#Class').val()
        }
    }).done(function(data) {
        $('#Result').text(data);
        alert(data);
    });
}
over 4 years ago · Santiago Trujillo Report

0

Try this:

function caller(){
          $.ajax({
              url: "test.php?Class_info=test",
              type: "get",
              data: {
                Class_info: $('#Class').val()
              }
          }).done(function(data) {
              $('#Result').text(data);
              alert(data);
        });
      }

I added ?info=test which in theory PHP should be able to take

if PHP takes the value, then instead of test put a variable containing the desired value

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!