jquery - Add text to hidden input value based on selection from dropdown -


although seems simple struggling find real solution. need add/append text value of hidden input based on selection of dropdown. value in dropdown added/appended randomly generated id suffix should "12345678_mybrand". easy do, need text (not entire value) change when dropdown selection changes rather adding/appending selected dropdown option.

here i'm doing

html:

<input id="myhiddenfield" type="text"> <select id="brand">     <option value="">please select</option>     <option value="first choice">first choice</option>     <option value="second choice">second choice</option>     <option value="third choice">third choice</option>     <option value="fourth choice">fourth choice</option> </select> 

jquery:

jquery(document).ready(function() {     var brand = $('#brand');     var hiddenfield = $('#myhiddenfield');     hiddenfield.val(1 + math.floor(math.random() * 10000000));      brand.change(function() {          hiddenfield.val( hiddenfield.val() + "_" + brand.val() );      }); }); 

https://jsfiddle.net/wo6a5nkf/2/

obviously don't want keep adding/appending suffix id how change text without adding/appending every single option selected???

you may use split:

jquery(document).ready(function() {  	var brand = $('#brand');    var hiddenfield = $('#myhiddenfield');    hiddenfield.val(1 + math.floor(math.random() * 10000000));        brand.change(function() {        	hiddenfield.val( hiddenfield.val().split('_')[0] + "_" + brand.val() );        });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <input id="myhiddenfield" type="text">  <select id="brand">  <option value="">please select</option>  <option value="first choice">first choice</option>  <option value="second choice">second choice</option>  <option value="third choice">third choice</option>  <option value="fourth choice">fourth choice</option>  </select>


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -