0

I have been searching for quite a while now, but can not seem to find a comprehensive answer. I have the HTML code below and would like to add the typed in value of the input name="ytcustom" to option value="https://url.com/custom/"

What is a solid way to accomplish this?

<select id="youtube_dropdown">
<option value="https://url.com/mix/">Mix</option>
<option value="https://url.com/custom/">Custom</option>
</select>
<input name="ytcustom" id="ytcustom" type="text" class="ythide">
4
  • can you explain what you are trying to achieve here Commented Mar 28, 2018 at 9:53
  • So when someone types a value into the textfield, i want that value to be added to the option value url.com/custom + val() Commented Mar 28, 2018 at 9:57
  • You first need to think of the event on which you want to add the input text as an option. For instance, if the user types a value in the enter box and hit enter, then add this value to the select box or when the input box looses focus then add the value to the select. Commented Mar 28, 2018 at 9:59
  • Yep, i would like it to be added while typing. Commented Mar 28, 2018 at 10:09

3 Answers 3

1

Here is the final version of my answer: using onkeyup event and using #youtube_dropdown > option:last to select an option element (although I would use ID selector if possible):

$(function() {
  $("#ytcustom").on("input", function() {
    var customUrl = 'https://url.com/custom/' + $(this).val();
    $("#youtube_dropdown > option:last").val(customUrl);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="youtube_dropdown">
<option value="https://url.com/mix/">Mix</option>
<option value="https://url.com/custom/">Custom</option>
</select>
<input name="ytcustom" id="ytcustom" type="text" class="ythide">

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

6 Comments

Cool, this works. Is it also possible to add the textfield value without using a ID? Also do i need a button to fire the jquery? Or can i also add it instantly?
I've edited my answer. Please check if it does answer your questions
Hi, thanks! Your last code comes really close, however when chaning the input txt field it ADDs the data to the url instead of start over (without the previous textfield content).
So when you enter 'ABCD' to the input field, you want option's value to be 'ABCD' instead of 'url.com/custom/ABCD'?
No. i want it to be 'url.com/custom/ABCD'; but when i type ABCD then focusout, clean the txtfield and type ABCD again the value becomes 'url.com/custom/ABCDABCD';
|
0

Remove and add attribute of custom option on text box focusout .

$("#ytcustom").focusout(function() {
  var str = $('#ytcustom').val();
  var lastValue = $('#youtube_dropdown option:last-child').val();
  var newValue = lastValue + str;
  $('#youtube_dropdown option:last-child').removeAttr('value');
  $('#youtube_dropdown option:last-child').attr('value', newValue);
  console.log($('#youtube_dropdown option:last-child').val());


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="youtube_dropdown">
<option value="https://url.com/mix/">Mix</option>
<option value="https://url.com/custom/">Custom</option>
</select>
<input name="ytcustom" id="ytcustom" type="text" class="ythide">

5 Comments

Very nice! Only when i type 'Test' it adds 'ttetestest' How come?
See Updated answer
Hi, Is it necessary to fire it when i click outside the textfield? Also when changing the textfield value later, it ADDS the content again instead of using a 'clean sheet'..
Its depend on when you want change URL and next time when you change text after clear your text box it's take new value at all.
So now it adds the textfield input but won't clear it. It keeps on adding.
0

Try This updated version

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#btnAdd").click(function(){
        var val=$('#ytcustom').val()
$("#optionID").val('https://url.com/custom/' + val);
            val=$('#ytcustom').val('');
            alert('Data Added');
        });
    });
    </script>
    </head>
    <body>

    <select id="youtube_dropdown">
    <option value="https://url.com/mix/">Mix</option>
    <option id="optionID" value="https://url.com/custom/">Custom</option>
    </select>
    <input name="ytcustom" id="ytcustom" type="text" class="ythide">
    <button id="btnAdd">ADD</button>

    </body>
    </html>

Without Click Button

<!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#ytcustom").change(function(){
        var val=$(this).val()
$("#optionID").val('https://url.com/custom/' + val);
            val=$(this).val('');
            alert('Data Added');
        });
    });
    </script>
    </head>
    <body>

    <select id="youtube_dropdown">
    <option value="https://url.com/mix/">Mix</option>
    <option id="optionID" value="https://url.com/custom/">Custom</option>
    </select>
    <input name="ytcustom" id="ytcustom" type="text" class="ythide">
    <button id="btnAdd">ADD</button>

    </body>
    </html>

5 Comments

@RobbTe try this one
Hi, this adds an extra option. I would like the value to be added to the option value of <option value="https://url.com/custom/ + value of textfield
Cool, can this also be done without using a option ID and without using a button to add the data?
yes.. you can do it on =$('#ytcustom').change(function(){});
Hi sorry, not fully. I understand the change function can replace the click function but not how to use the option value, instead of optionID. Since i don't have an optionID i rather user its value.

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.