Jump to content

function like attr("id") but for multiple?


shortysbest

Recommended Posts

yes jQuery.

 

This is the code that i am trying to get the id from.

<div class="attendance-presence">
<select class="attendance-box" id="<?php print $row['id'];?>" onchange="send_attendance(this.value);">
<option selected>--Select--</option>
  <option value="1">Present</option>
  <option value="2">Tardy</option>
  <option value="3">Absent</option>
</select>
</div>

 

This is the javascript i am using to get the value of the option selected which works perfectly fine,

 

function send_attendance(value)
{

####/////This right here works, but it only displays the id for the very first item in the list. need it for any of the ones selected
var id = $(".attendance-box").attr("id");


$.ajax({
type: "POST",
url: "ajaxpages/attendance/send_attendance.php",
data: "student_id="+value,
cache: false,
success: function(html){
alert(id);

$(this).html('attendance sent');

}
});    
}

Link to comment
Share on other sites

jQuery allows (and promotes the usage of) unabtrusive Javascript, so remove the onChange() event from your markup.

 

Not tested but something like....

 

$(document).ready(function() {
  $(".attendance-box").change(function() {
    var id = $(this).attr('id');
    $.ajax({
      type: "POST",
      url: "ajaxpages/attendance/send_attendance.php",
      data: {
        student_id: $(this).val()
      }
      cache: false,
      success: function(html) {
        alert(id);
        $('#message').html('attendance sent');
      }
    });
  });
});

 

should get you pretty close.

Link to comment
Share on other sites

yeah, but i fixed it to work in the external sheet. I just had to add the javascript linking code to the page (<script type="text/javascript" src="javascript/ajax.js"></script>) since the page I'm working on is being called by jquery once before into the page to begin with. It's not actually being written in the code to the index page where the links are initially made.

 

however with this:

 

$(document).ready(function() {
  $(".attendance-box").change(function() {
    var id = $(this).attr('id');
    alert(id);
  $.ajax({
      type: "POST",
      url: "ajaxpages/attendance/send_attendance.php",
      data:  "student_id="+$(this).val(),
      cache: false,
      success: function(html) {
        alert($(this).val());
        $('#message').html('attendance sent');
      }
    });
  });
});

 

it doesn't alert out the val() of the option i clicked. The way you had the data: before there was an error with it, and i need "student_id=" to be part of it so another external page can get the value of that to use

Link to comment
Share on other sites

Within a function 'this' has different meaning.

 

You will need to retrieve the value outside of your success() callback....

 

$(document).ready(function() {
  $(".attendance-box").change(function() {
    var id = $(this).attr('id');
    var val = $(this).val();
    $.ajax({
      type: "POST",
      url: "ajaxpages/attendance/send_attendance.php",
      data: {
        student_id: val
      },
      cache: false,
      success: function(html) {
        alert(val);
        $('#message').html('attendance sent');
      }
    });
  });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.