0
<div class="row" id="gas_analyte1">
              <div class="col-md-3">
                Gas Analyte 1<br>
                <%= f.select :gas_analyte, @Gases, {include_blank: 'Select Gas', size:15},{style: "width:150px;",class: "gas_search"} %><br>              
              </div>
</div>

@Gases is an array of gases such as carbon monoxide that is created in the controller.

In Javascript, if a user clicks on the add button, I would like to add another select box to the input page so I have this so far

$('#gas_analyte_btn').click(function(){
    $('#gas_analytes').append('<div id="gas_analyte'+(i).toString()+'" class="row"><div class="col-md-3">Gas Analyte '+(i).toString()+'<br><input size="15" type="text" name="search[gas_analyte'+(i).toString()+']" id="search_gas_analyte'+(i).toString()+'" />
});

The problem I having is that the javascript created a Textarea and not a select option. How can I create a select option that I can select from @Gases in the Javascript?

Thank you

2
  • If you want to use select option you should rather use select and option tags Commented Nov 18, 2019 at 2:47
  • You could also check out the cocoon gem. It helps with adding fields to a form dynamically. github.com/nathanvda/cocoon Commented Nov 18, 2019 at 8:24

2 Answers 2

1

$('#gas_analyte_btn').click(function(){
  let value = $(this).siblings('input[type="text"]').val();
  if (value.length > 0) {
    $('#myselect').append($('<option>'+value+'</option>'))
                  .val(value); // selects the new value
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="row" id="gas_analyte1">
  <div class="col-md-3">
    <select id="myselect">
      <option>Helium</option>
      <option>Hydrogen</option>
      <option>Oxygen</option>
    </select>
  </div>
  <div class="add_option">
    <input type="text"/>
    <button id="gas_analyte_btn">Add Option</button>
  </div>
</div>

Also you can programatically create HTML elements and fragments using jQuery or just the vanilla DOM instead of that completely unreadable string concatenation concoction:

var id = i.toString();
var $in = $('<input>').attr({ 
  name: 'search[gas_analyte'+i+']',
  id: 'search_gas_analyte'+i
});
# ...
Sign up to request clarification or add additional context in comments.

Comments

1

I cant understand your question clearly but I think this is what you are looking for . Giving you an example of which you can safely extrapolate. Cheers.

<html>
<head>
</head>
<body>
<select id="sltGas" onchange="alert(this.options[this.selectedIndex].text);">
  <option>Hello</option>
</select>
<input type="text" id="txtGas" name="txtGas"><input type="button" name="btnAdd" id="addBtn" value="ADD" onclick="addGas();">
<script type="text/javascript">
var sltGas = document.getElementById("sltGas");
alert(sltGas.options[sltGas.selectedIndex].text);

function addGas()
{
 var txtGas = document.getElementById("txtGas");
 var option = document.createElement("option"); 
 option.text = txtGas.value; 
 option.value = 2;
 sltGas.add(option) 
}
</script>
</body>
</html>

Comments

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.