0

is there an easy way to update an html property from an HTML select object ? in the example below I would like to update name with the value from the select object when the user chooses an option.

EDIT: user can add more search criteria like below; I have a function to add similar code like below to the search form.

<input id="AS1" class="searchCriteriaInput" type="text" name="" value="" />
<select>
    <?php
        foreach ($fields as $key => $value) {
            if ($key=="select") {
                echo "<option selected value='".$key."'>".$value."</option>";
            } else {
                echo "<option value='".$key."'>".$value."</option>";
            }
        }
    ?>
</select>
7
  • Ajax with jquery is an option to make a call to a php page with option Commented Jan 11, 2018 at 17:21
  • @L.Ros. that's not what the OP is asking Commented Jan 11, 2018 at 17:22
  • you need client side programming for this. Commented Jan 11, 2018 at 17:22
  • Assuming from your tags you're happy with a jQuery solution, you can hook to the change event of your select, then get it's val() and set that as the value of the input. Commented Jan 11, 2018 at 17:23
  • @Rory McCrossan Ok. Commented Jan 11, 2018 at 17:23

3 Answers 3

0

This does what you ask, although it is not impressive to look at in the snippet, however, inspecting the input field after change shows that the name attribute has been populated.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="AS1" class="searchCriteriaInput" type="text" name="" value="" />
  <select onChange="doThing(this)">
    <option value="KeyOne">ValOne</option>
    <option value="Key2">Val2</option>
    <option value="Key3">Val3</option>
    <option value="Key4">Val4</option>
    <option value="Key5">Val5</option>
  </select>
  
  
  <script type="text/javascript">
    function doThing(f){
      $("#AS1").attr("name",$(f).val());
    }
  </script>

Sign up to request clarification or add additional context in comments.

4 Comments

that looks perfect ! Thanks I'll try now
works great for the first one and it answers the question but Im trying to adapt your answer to the dynamically added ones without any luck so far; this is not found. I tried replacing with e.target and a dynamically generated id for the select object but doThing is not updating name. any idea ?
@jotyhista I would maybe have to see it. the this of course refers to the select element. you can give it an id and then change the function. the option values being generated dynamically should not change anything as they are rendered prior to the javascript running.
I posted a new question here in case you have time to spare: stackoverflow.com/questions/48214933/…
0

You can use jquery. Bind the on change event for select and set select value to required place. for your example

<input id="AS1" class="searchCriteriaInput" type="text" name="" value="" />
  <select class="sel">
      <?php
        foreach ($fields as $key => $value) {
          if($key=="select"){
            echo "<option selected value='".$key."'>".$value."</option>";
          }else{
            echo "<option value='".$key."'>".$value."</option>";
          }
        };
      ?>
  </select>
$(document).ready(function(){
$('.sel').change(function(){
var value = $(this).val();
$('#AS1').val(value);
});
})

Don't forget to add jquery library

Comments

0

You don't need php for it. You can do it using javascript (Jquery). Here's an example:

$(document).ready(function() {

    $("#select_name option").filter(function() {
        return $(this).val() == $("#animal_name").val();
    }).attr('selected', true);
    $("#select_name").on("change", function() {     
  $("#animal_name").attr("name",$(this).find("option:selected").attr("value"));
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select_name" name="select_name"> 
<option value="">Please select an animal</option> 
<option value="Cat">Cat</option> 
<option value="Dog">Dog</option> 
<option value="Cow">Cow</option> 
</select>

<input type="text" id="animal_name" name="animal_name" value="" readonly="readonly">

1 Comment

the question asks to update the attribute name in the input field, not the value. [I think]

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.