shortysbest Posted September 24, 2010 Share Posted September 24, 2010 $(".attendance-presence").attr("id"); How would i get it to get the id of the list of items that has the class of attendance-presence for the element clicked? this just returns the first id in the list no matter which one i click. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/ Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 jQuery I assume? Can we see some sample html? Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115355 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 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'); } }); } Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115365 Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115374 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 Thanks, but this code doesn't work. I have tried changing it around some but still nothing. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115380 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 There must be a function to replace attr() that does about the same thing, just with a list of items? This code below works perfectly fine, only it only displays the first id of the list. $(".attendance-box").attr("id"); Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115384 Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 Thanks, but this code doesn't work. I have tried changing it around some but still nothing. It is working fine for me. See example: http://jsbin.com/eyuvi4 Your ajax call might be failing. Are you debuguing using firebug? Any errors? Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115389 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 yeah, but I have an array of anywhere between 10 and 25 of those menus, each having a different id assigned to them, that just shows it with one, which i have been able to get to work as well. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115390 Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 Still working with 2 of them. http://jsbin.com/eyuvi4/2 The first will show an id of foo while the second will be bar. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115393 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 yeah i tested two on there too, it worked fine for when i did it too, however i put the exact code in mine and it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115394 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 oh i see, i had my javascript in an external file. I put it in the same file and it worked. I'm having problems with the data sending part of it tho Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115395 Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 Its in an external file in the paste bin we are using. That makes NO difference. Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115396 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115402 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 take that back, just made $(this).val(); into a single variable and now it works perfectly. Thanks alot for your help with this whole part. Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115404 Share on other sites More sharing options...
trq Posted September 25, 2010 Share Posted September 25, 2010 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'); } }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115406 Share on other sites More sharing options...
shortysbest Posted September 25, 2010 Author Share Posted September 25, 2010 yeah thats exactly what i did Quote Link to comment https://forums.phpfreaks.com/topic/214326-function-like-attrid-but-for-multiple/#findComment-1115407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.