simrantechy Posted July 6, 2022 Share Posted July 6, 2022 (edited) On 6/28/2022 at 6:08 AM, Barand said: Use data attributes for the options <?php $data = [ [ 'id' => 1, 'rifleName' => '.308 Bolt action', 'em' => '225', 'gm' => '1350' ], [ 'id' => 2, 'rifleName' => '7mm magnum', 'em' => '300', 'gm' => '1575' ], [ 'id' => 3, 'rifleName' => '.243 LeverAction', 'em' => '215', 'gm' => '8725' ] ]; ?> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $().ready(function() { $("#menu").change( function() { let em = $(this).find("option:selected").data("em") let gm = $(this).find("option:selected").data("gm") $("#em").val(em) $("#gm").val(gm) }) }) </script> <select class='nameItems' id="menu" name="selection"> <option value="">Choose a Rifle</option> <?php foreach ($data as $row) { echo "<option value={$row['id']} data-em='{$row['em']}' data-gm='{$row['gm']}'>" . htmlspecialchars( $row["rifleName"] ) . "</option>"; } ?> </select> em:<input type="text" class="charge-type" name="em" id="em" value="0" disabled size="5"/> gm: <input class="charge-type" name="gm" id="gm" value="0" disabled size="5"/> Not working for me, Was reading this post but fixing this but not able to do that. Edited July 6, 2022 by cyberRobot Linked to post referred to by OP Quote Link to comment https://forums.phpfreaks.com/topic/315005-select-options-using-data-attributes/ Share on other sites More sharing options...
Barand Posted July 6, 2022 Share Posted July 6, 2022 Hijacking someone else's topic aside for now, define "Not working". Quote Link to comment https://forums.phpfreaks.com/topic/315005-select-options-using-data-attributes/#findComment-1598008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.