0

I want to GET ID or class of the clicked element. Can someone give me idea how to do this? I tried to get the ID of the clicked element but it's not working.

Here's my jquery.

$('.sems').click(function() { 
    var id = $(this).attr('id');
    alert(id);
});

Here's the full code where link is located.

if(isset($_POST['loadsem'])) {
    $stud = $_POST['stud'];
    $output = '';  
    $sql = "SELECT DISTINCT sch_year,semester FROM grades WHERE stud_no ='$stud'";
    $result = mysqli_query($con,$sql);
    $output .= '
                  <div class="table-responsive">
                    <table class="table table-bordered">
                      <tr>
                        <th>Semesters Attended</th>
                      </tr>';
      while($row = mysqli_fetch_array($result))
      {
        $sem = $row['semester'];
        $year = $row['sch_year'];
        $output .= '<tr>
                      <td><a href="#" class="sems" id="asd">'; <--- Here's the link
                    if($sem == "1"){
                      $output .= $row['semester']. "st Semester S.Y " .$row['sch_year'];
                    } else {
                      $output .= $row['semester']. "nd Semester S.Y " .$row['sch_year'];
                    }
                    $output .='</a></td>
                    </tr>';
      }
          $output .= '</table>
            </div>';
   echo $output;    
}
Nurjan
  • 5,889
  • 5
  • 34
  • 54
nethken
  • 1,072
  • 7
  • 24
  • 40

2 Answers2

3

Seem you need to delegate the event

 $('body').on('click','.sems',function(event) { 
    var _id = $(this).attr('id');
    alert(_id);
    })

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

brk
  • 48,835
  • 10
  • 56
  • 78
  • Can you explain why my code is working and yours is working? – nethken Dec 21 '16 at 05:57
  • 1
    the both code perform same action , in your code action target direct to element but in this code it will search the element within the body tag if found then target the element and perform click. – Soni Vimalkumar Dec 21 '16 at 06:02
  • @Soni and what's about this "_" in the variable name "_id" when i removed that the code's not working anymore? hmmm. – nethken Dec 21 '16 at 06:04
  • *"Seem you need to delegate the event"* - Why? Remember that beginners will read this answer. – nnnnnn Dec 21 '16 at 06:04
  • I need further explanation because i'm just a beginner and don't understand complex code properly. – nethken Dec 21 '16 at 06:05
  • 1
    `_id` is just variable name. It is not same as `id`. You can name it anything you want – brk Dec 21 '16 at 06:06
  • 1
    _id is a name of variable , instead of that you can directly use the element **OR** its property, No need to save in other variable. **(e.g: alert($(this).attr('id')))** – Soni Vimalkumar Dec 21 '16 at 06:06
  • Okay guys~ Thankyou so much! :) – nethken Dec 21 '16 at 06:10
0

You can try:

$('body').on('click','.sems',function(event) { 
   var id = $(this).prop('id');
    alert(id);
});

Hope this will help you...

Diff betweeb prop and attr - link

you should follow this link also.

Community
  • 1
  • 1
Devansh
  • 1,277
  • 1
  • 13
  • 19