I was wondering how would i get like an id value so i could do something like this, I have 2 fields and a div (status).
<input name="item_1" type="hidden" value="1" />
<input type="text" name="date_1" id="dateselect" onchange="check();" />
<div id="status_1">Click on the field above!</div>
<input name="item_2" type="hidden" value="2" />
<input type="text" name="date_2" id="dateselect" onchange="check();" />
<div id="status_2">Click on the field above!</div>
The value in the hidden field will be the same number for likes of item_x and date_x etc. that is 2 sets of field and there would be alot more! coded with PHP (foreach item).
So i need to pass the values of these fields into a ajax request.
date = $("[name='date']").val();
item = $("[name='item']").val();
elm = "#status";
How would i get the “1” or “2” as a var called id, so i could do something like
date = $("[name='date_"+id+"']").val();
item = $("[name='item_"+id+"']").val(); or item = id;
elm = "#status_"+id;
Since there will be multiple of these mini checkers, doing the same but not at the same time, only when clicked. I can easily do it when there is only 1 but when there is like more then 1 im a little stuck.
Heres my version of it doing it once, i need to some how set the id, so its no longer undefined:
function check(id,elm) {
book = $("[name='date']").val();
var item = '';
if(id === undefined){
item = $("[name='item']").val();
}else{
item = id;
}
if(elm === undefined){
elm = "#status";
}
if(book.length >= 3 && item.length >= 0) {
$(elm).html('<img align="middle" src="images/loading.gif" alt="Checking" /> Checking availability...');
$.ajax({type: "POST",url: "checker.php",data: "date="+book+"&itemid="+item,success: function(msg){
$(elm).ajaxComplete(function(event, request, settings){
if(msg == 'OK') {
$(elm).html(' <img align="middle" src="images/tick.png" alt="Available" /> Available!');
$(elm).next().val(1);
} else {
$(elm).html(msg);
$(elm).next().val(0);}
});
}});
} else {
$(elm).html('');
}
}
Thanks for any help
Based on your markup without the
onchangeeventYou can let jQuery do the event wiring, for starters, and use the proper event (
onkeypressoronblurwould be best, I think)