0

I need to set value of text box on change from select box created using jquery

 <?php for($i=0;$i<5 ;$i++) { ?>
<select  id="<?php echo 'aaa'.$i ?>" class="<?php echo 'aaa'.$i ?>">
                    <?php for($i=0;$i<5 ;$i++) { ?>
  <option value="1112" data-xyz="dynamic_value " data-abc="dynamic_value">dynamic_value</option>
            </select>
             <input  type="hidden"  class="<?php echo 'bbb'.$i ?>" id="bbb" name="<?php echo 'bbb'.$i ?>"/>
            <input type="hidden"   class="<?php echo 'ccc'.$i ?>" name="<?php echo 'ccc'.$i ?>" id="ccc" />
                       <?php } ?>
                         <?php } ?>






            <script>
$('.aaa').change(function () {
var otherValue=$(this).find('option:selected').attr('data-xyz');
var someOtherValue=$(this).find('option:selected').attr('data-abc');
$('.bbb').val(otherValue);
$('.ccc').val(someOtherValue);
});
</script>

How to change value class bbb0-bbb5 in jquery without using loop in jquery

1
  • Why are you giving us PHP code? Just give the source HTML. Commented Jun 27, 2016 at 18:33

1 Answer 1

1

Do not update the class names in php-loop, classes need not be unique.

To select input:hidden elements, no need to specify ID attribute

$('.aaa').change(function() {
  var otherValue = $(this).find('option:selected').attr('data-xyz');
  var someOtherValue = $(this).find('option:selected').attr('data-abc');
  $(this).siblings('.bbb').val(otherValue);
  $(this).siblings('.ccc').val(someOtherValue);
});
<?php for($i=0;$i<5 ;$i++) { ?>
<select id="<?php echo 'aaa'.$i ?>" class="aaa">
  <?php for($i=0;$i<5 ;$i++) { ?>
  <option value="1112" data-xyz="dynamic_value " data-abc="dynamic_value">dynamic_value</option>
</select>
<input type="hidden" class="bbb" name="<?php echo 'bbb'.$i ?>" />
<input type="hidden" class="ccc" name="<?php echo 'ccc'.$i ?>" />
<?php } ?>
<?php } ?>
Sign up to request clarification or add additional context in comments.

3 Comments

$(this).next('.ccc') won't work, since the ccc element isn't the next one. I think you mean $(this).siblings('.ccc').
@Barmar Right! Have updated it.. Was curious about better approach if all the elements are in same level..without any parent.. .nextAll('.css:first') ?
That's another way to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.