I have a list of items and when a user clicks on one of them, it gets assigned a class called 'active-child'. Just one item could be selected at the moment, and when the user clicks on another item, this item is cleaned of 'active-child' class and then this class gets assigned to the newly selected item. Just like any other non-multiple list.
Here is the selected element's HTML:
<div id="5" class="new-child-item new-child-item-active active-child"> Software </div>
When I want to submit the selected item's "id" to a php script to process it further, the jquery does not retrieve the id value of the element and says it is undefined
. My approach to getting the selected item's id
attribute is as follows:
$('.active-child').attr('id');
I also have tested this, but still returns undefined
:
$('.active-child:first-child').attr('id');
The strange thing is that I first check to see if the user has selected anything at all, and if yes, then do the ajax, at this moment, the jquery returns 1
, means the user has selected one item. Here is the code block which checks to see the selection and then send it through ajax:
if($('.active-child').length > 0)
{
$('.new-item-cats-list-holder').empty();
$('.new-item-cats-list-holder').hide()
$('.big-loader').show();
console.log("This is the id: " + $('.new-child-item-active:first-child').attr('id'));
$.ajax({
url : base_path('ajax/set_credentials/step_2'),
type: "POST",
data : ({'set_sess':true, 'category' : $('.active-child').attr('id')}),
success : function(msg){
if(msg!=0)
{
//window.location.href = base_path('panel/add/step/ads/3');
}
},
});
}// end of length if