0

I am using a select function with a 2 level filter, and i can copy the result and have it display in another box using a script, however i dont want the value in the option value field i want the actual text displayed which in my case has to be different

<select name="selectbranch" id="selectbranch">
    <option selected="selected" value="">-Select-</option>
    <option value="1">Basildon</option>
</select>

<select name="branch" id="branch">
    <option selected="selected" value="">-Select-</option>
    <option value="Basildon">staff1</option>
</select>

<input type="text"  id="branchstaff" name="branchstaff" placeholder="branchstaff" />

<script>
function sync() {
    var sel = document.getElementById("branch");
    var text = sel.options[sel.selectedIndex].text;
}
</script>

however the result is Basildon but i want the result to be staff1

takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • I cant seem to get it working, can you help edit what i have to make it work? – Carl Hussain Mar 28 '18 at 14:41
  • "_I cant seem to get it working_" Show the code you wrote using the answer from the duplicate question and maybe we can see what is wrong. – takendarkk Mar 28 '18 at 14:49
  • when i change branchstaff.value = branch.value; to branchstaff.value = branch.text; i get undefined – Carl Hussain Mar 28 '18 at 15:10
  • Where is the element with the id `branchstaff`? It does not exist in your example here. – takendarkk Mar 28 '18 at 15:12
  • its just below as an input – Carl Hussain Mar 28 '18 at 15:13
  • You should update your question to provide a full example which does what you are explaining. Also, your "update" is not accurate with what is described in the answer of the linked duplicate question - that answer gets the list of options, finds the selected one, etc... you just changed `.value` to `.text` which will not work. – takendarkk Mar 28 '18 at 15:14
  • I simply want the Text from this - which is staff1, not the value- as i am using the value for something else - now i know the script has something to do with copying the contents of the text field and not the value, with something like var branch = document.getElementById('branch'); var branchstaff = document.getElementById('branchstaff'); branchstaff.value = branch.value; but this only pulls the value - Basildon and not the text, staff1 , if i can change it to pull text, im sorted – Carl Hussain Mar 28 '18 at 15:16
  • The answer to how to do that is in the question I linked you to - you just haven't done that in your code yet. – takendarkk Mar 28 '18 at 15:17
  • Yes but i dont know how to pull the text instead of the value – Carl Hussain Mar 28 '18 at 15:18
  • That is **exactly** what the answer to the other question explains - I don't know how to make that clearer. Read that answer, implement that in your code, done. I am not going to write it for you. – takendarkk Mar 28 '18 at 15:19
  • The answer doesnt work, ive tried several pages and its not working. i dont get why you cant just help me and explain why so i can learn and pass on the knowledge in the future? – Carl Hussain Mar 28 '18 at 15:22
  • its like saying, look at that person driving, do that - when i dont know how to drive... – Carl Hussain Mar 28 '18 at 15:23
  • "_The answer doesnt work_" Yes, it does. Show the code you wrote (in your question, not a comment) where you get the select element, get the selected option, then get the text as described in that answer. As I already explained, it seems all you did was just change `.value` to `.text` which is not what is described in that answer. – takendarkk Mar 28 '18 at 15:24
  • all of that is in my intial question, if you wont answer and tell me " the answer is there look" its not going to help is it? if i dont get it , then i wont get it unless you explain. – Carl Hussain Mar 28 '18 at 15:26
  • "_all of that is in my intial question_" I don't see in your initial question where you get the selected option of the select element like in the other answer here `sel.options[sel.selectedIndex]`. All I see is you getting the select element. – takendarkk Mar 28 '18 at 15:28
  • I DO NOT KNOW HOW, if i did i would never have posted on here??!?! how does that make no sense to you – Carl Hussain Mar 28 '18 at 15:30
  • `sel.options[sel.selectedIndex]` <- this _is_ how – takendarkk Mar 28 '18 at 15:31
  • Ive edited it to the best of my knowledge but i cant see how i would get the result into my textfield below? – Carl Hussain Mar 28 '18 at 15:34
  • [Set the value of an input field](https://stackoverflow.com/questions/7609130/set-the-value-of-an-input-field) - `document.getElementById("branchstaff").value = text;` – takendarkk Mar 28 '18 at 15:39
  • ive made the suggested changes, but the value of an input field? how do i tell the script to put the selectedindex text into a textarea? that link you provided just explains how to set the value of a select field – Carl Hussain Mar 28 '18 at 15:42
  • where would u put that? document.getElementById("branchstaff").value = text; in the script? – Carl Hussain Mar 28 '18 at 15:43
  • You don't have a textarea, you have an ``. The link describes how to set the value of such an input field. Put that line right after you make the `text` variable in the script. – takendarkk Mar 28 '18 at 15:43
  • Its working now! – Carl Hussain Mar 28 '18 at 15:44
  • thank you for your help and patience, i know the old saying teach a man to fish etc, but if he doesnt even have a fishing rod he isnt getting far :) – Carl Hussain Mar 28 '18 at 15:44
  • It's like pulling teeth but you will remember this much better than if I just wrote it for you and you copy/pasted - I promise. – takendarkk Mar 28 '18 at 15:45

0 Answers0