L
L
leoZZ2021-07-03 16:54:29
JavaScript
leoZZ, 2021-07-03 16:54:29

How to bind select and button?

Good afternoon! Help me please. There is the following code snippet:

<div class="right_info">
    <h2>Список файлов</h2>
      <select id="list_select" multiple>
        {% for i in files %}
            <option id="{{ forloop.counter }}">{{ i.name }}</option>
        {% endfor %}
      </select>
    <button class="button_right" type="button" onclick="WidgetLink(this)">Открыть</button>
    <button class="button_right" class="button_right" type="button">Скрыть/Показать</button>
</div>


Select is populated with options automatically based on Django(python) database. What you need to do is to be able to click on a certain option, and then click on the "Open" button, get the name of this option and open a new window. There is code that opens a new window based on the name it receives. But this code was tied to something else, I don’t know how to figure it out with selects.
function WidgetLink(obj) {
  let name = document.getElementById(obj.id).innerText;
  let link = window.open('http://127.0.0.1:8000/widget/'+name, '_blank');
};


Here's what it looks like from the side:60e06ad415620142525948.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tyoma_koder, 2021-07-03
@leoZZ

The value of the selected option is stored in the value property of the select, when you click on the button, you get select.value and that's it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question