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

theUnforgiven's avatar

Get custom attr value & add to hidden input

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?

0 likes
15 replies
d3xt3r's avatar

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

tykus's avatar

What exactly are you trying to do? Can you provide some of your code?

theUnforgiven's avatar

Just wanting to get the custom attribute value from data-optionId

Then append an hidden input with this value.

The rendered HTML is as follows:

<option v-model="option_id" value="609-Black" id="optionId" data-optionId="1345">Black  - In Stock </option>
<option v-model="option_id" value="609-Raspberry" id="optionId" data-optionId="1347">Raspberry  - In Stock </option>
theUnforgiven's avatar

Currently reviewing the events in Vue put not sure how to get the custom attribute value.

d3xt3r's avatar

@lstables Edit my previous answer , misspelled without as with (my bad) which might have made you think otherwise. If you are using plain javascript (or even jquery) , its quite simple.

theUnforgiven's avatar

Ok with jQuery which is what I thought how would I achieve this, (don't do JS) has you've probably gathered but should be quite simple for someone like you buddy!!

theUnforgiven's avatar

Tried the following:

$('.colors').on('change', function(e) {
            var option_id = $("#optionId").data('optionId');
            var test = $("#pid").append($('<input type="hidden" name="pid" />').val(option_id));

            alert(test);
        });

But the alert shows empty [object]{Object]

Dropdown has the following:

<select name="colour" class="browser-default colors">
willvincent's avatar
Level 54

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.

1 like
theUnforgiven's avatar

@ willvincent That's just what I needed wasn't quite sure on how to write it, but I guess I was on the right tracks in a way... lol

Thanks again :)

willvincent's avatar

@lstables FYI...

But the alert shows empty [object]{Object]

alert's don't show object details, so that's not necessarily an empty object, it's just telling you that the var is an object .. you should use console.log() instead of alert() for dev stuff like this. ;)

1 like

Please or to participate in this conversation.