Not possible with(out) jquery or javascript as its client side processing. Listen to on-change event for your drop down and then propagate it to your hidden field? On a separate note, still waiting :P
Edit: I meant without
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How I can get the custom attribute of data-optionId thats been selected from a dropdown menu, then add it to a hidden field?
Quickest easy, less jQuery way possible, if anyone can advise?
vue should have bindings for that sort of thing..
Taking vue out of the picture completely though, something like this should work:
Markup:
<form id="foobar">
<select name="foo" id="foo">
<option value="1" data-optionId="foo">One</option>
<option value="2" data-optionId="bar">Two</option>
<option value="3" data-optionId="baz">Three</option>
</select>
</form>
JS:
(function($) {
$(document).ready(function() {
$('select#foo').bind('change', function() {
// Fetch selected item's data element value
var optionId = $(this).children(':selected').attr('data-optionId');
// Remove any existing hidden input
$('input[name="hidden-option-id"').remove();
// Append new hidden input element
$('<input>').attr({
type: 'hidden',
id: 'hidden-option-id',
name: 'hidden-option-id',
value: optionId
}).appendTo('form#foobar');
});
});
})(jQuery);
See it working in this plnkr.
Please or to participate in this conversation.