1

I'm trying to add <input name="input"> values to <select> <options> each time when I change the value with onblur function.

In this case it does change the value of <input name="output"> but I would like to add them multiple, so that's why I'm looking for adding values in to select options.

Is this possible?

Thanks.

function add(form) {
		form.output.value = form.input.value;
	}
<form>
  <input name="input" type="text" value="" onblur="add(this.form);">
  <input tabindex="-1" name="output" type="text" value="">
  <select multiple>
    <option>Test</option>
  </select>
</form>

2 Answers 2

2

if you mean that after changing input value , add it's value as option to select , yea it's possible I've wrote the code see the demo at below link

https://jsfiddle.net/oxxqw71s/

and code :

$("select").append($('<option>', {value:v, text:v}));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Yes this is what I was looking for. Is there any chance to make it reverse, I mean if I want to take the value out from select if I add mistakenly just in case.
Yes can get when select value changes just add this attr onchange="selectchanged(this.value)" to select then in javascript declare function with name selectchanged like below function selectchanged(v) { //v is the value of select }
1

You can implement like below.

function add(val){
  var data = val.input.value;
  val.output.value = data;
  
  var option = document.createElement("option");
  option.text = data;
  val.opt.add(option, val.opt[0]);
  
}
<form>
  <input name="input" type="text" value="" onblur="add(this.form);">
  <input tabindex="-1" name="output" type="text" value="">
  <select multiple name="opt">
    <option>Test</option>
  </select>
</form>

HTML

<form>
  <input name="input" type="text" value="" onblur="add(this.form);">
  <input tabindex="-1" name="output" type="text" value="">
  <select multiple name="opt">
    <option>Test</option>
  </select>
</form>

JS

function add(val){
  var data = val.input.value;
  val.output.value = data;

  var option = document.createElement("option");
  option.text = data;
  val.opt.add(option, val.opt[0]);

}

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.