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

PersonalHomePage's avatar

JQuery loop through multiple dynamic elements and change them at the same time

Hi, I hope someone can help me with the syntax how to write this iteration. I have rows of elements like so:

<input class="text-box" id="string-text-and-id-1234" type="text"> 
<input class="checkbox" type="checkbox" checked="checked" data-index-number="1234">

the 1234 part increments randomly. I need to iterate both of these input elements and if it's checked than add disable to the first input element (the text input).

I was trying this, but it's not working:

$('input.checkbox').each(function() {
// check that the number '1234' in data-index-number="1234" is equal to id="string-text-and-id-1234"
	if( $(this).data('index-number')  == $('input.text-box').attr("id").match(/\d+/)[0])
		// do some stuff
})

I'm not very familiar with jquery and can't really find what I need online. Would appreciate your help with this, thank you in advance.

0 likes
2 replies
SilenceBringer's avatar
Level 55

@personalhomepage like this

$('input.checkbox').each(function() {
	$('#string-text-and-id-' + $(this).data('indexNumber')).prop('disabled', $(this).is(':checked'));
});

Please or to participate in this conversation.