Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

how to Concatenate an id in a foreach loop using jquery laravel

i have a foreach loop whereby i want to concatenate the id of each loop to its id jquery.i have tried this but i get the the id for the first loop only.this is my code in the blade file.

 @foreach ($rentalcategories as $category)
  <div class="checkbox checkbox-success">
    <input type="text" value="{{ $category->id }}" class="catid">
    <input type="radio" name="rentalcategory" class="rentalcattitle" id="rentalcat{{ $category->id }}" value="{{ $category->id }}">
        <label for="rentalcat" class="control-label"> {{ $category->rentalcat_title }}</label><span>({{ $category->rentalhses->count() }})</span>
  </div>
@endforeach

this is my jquery code

var categoryid=$(".catid").val();
console.log(categoryid);
var hsecategory= $('#rentalcat' + categoryid).prop('checked') ? $('#rentalcat' + categoryid).value : ''

on the console logging the categoryid variable am only getting the first value only for the first loop but the other one it doesn't show.how can i get the id for each loop then concatenate to an id in each loop. here in my code i want to get the value for each loop using the id instead of class.how can i achieve this

0 likes
4 replies
sidanalavi's avatar

Could you explain what you are trying to achieve ?

1 like
stephen waweru's avatar

@sidanalavi what am trying to achieve is get the value of an input in the foreach loop using an id instead of a class

sidanalavi's avatar

I get it, Sorry for late response, So based on the id you will get the the value of the checked box and then assign it to variable is that it ?

sidanalavi's avatar

var categoryid=$(".catid").val(); // as you have already consoled it should give an array of values. console.log(categoryid); var hsecategory= $('#rentalcat' + categoryid).prop('checked') ? $('#rentalcat' + categoryid).value : '' What i wanted to know mainly what exactly this code will do because i think if you have to make it dynamic you should use "this" keyword as it is more efficient.

1 like

Please or to participate in this conversation.