-2

When I click on an option, I want the selected attribute to be applied to the clicked option and removed from the previously selected option.

Here is the html

<form action="" method="post" class="form" name="myForm">
    <select id="first" name="mySelect">
        <option class="my-choise" selected="selected">1</option>
        <option class="my-choise">2</option>
        <option class="my-choise">3</option>
        <option class="my-choise">4</option>
    </select>
</form>

And here is the javascript. Please note I am not using jQuery

 var objSel = document.forms[0].elements[0];
for (var i=0; i<objSel.options.length; i++) {
    console.log(document.getElementsByClassName('my-choise')[i]);
    objSel.options[i].addEventListener('click', function(){
        alert (ok);
        var id = document.getElementsByClassName('my-choise')[i];
        id.setAttribute('selected', 'selected');
    },false);
}
Kseniia
  • 3
  • 2
  • This doesn't make sense. Can you please reword your first paragraph to explain what you are trying to do please. – Mark Mar 21 '14 at 09:54
  • it already happens behind the scenes, why are you trying to spin a wheel thats already being spun by an engine? – Banana Mar 21 '14 at 09:54
  • So am I right in saying, you have two select elements that you want to keep in sync? So if you select an item in 1, the same item will be selected in the other? – Mark Mar 21 '14 at 09:56
  • Now option with id="item-1" is selected. For example, I want to click option with id="item-3" And from click I want to option with id="item-3" become selected and option with id="item-1" lost his selected property – Kseniia Mar 21 '14 at 09:57
  • kzh has a nice post on how to do this at http://stackoverflow.com/questions/7373058/how-to-change-the-selected-option-of-html-select-element – Mark Mar 21 '14 at 09:59

1 Answers1

0

it would absolutely make sense if you set the selected attribute on your 'select' element using another element like a button.

but if you click on the options in your same 'select' element, the options are being already selected behind the scenes and there is absolutely no reason for you to set the 'selected' attribute on the same 'select' element.

here is a demo for you in jsfiddle that shows that options are being set selected already automatically: DEMO

EDIT: to have your javascript execute after the html is loaded, put it in a function that will only run after the document is fully loaded:

document.onload=onLoadHandler;

function onLoadHandler(){
 //put your onclick handler attachment here
}
Banana
  • 7,424
  • 3
  • 22
  • 43
  • thank you! But I don't have any buttons. I must change selected property. Now the element width id="item-1" is selected. For exaple, I click on element with id="item-3". And I want to element with id="item-3" have selected, but not element with id="item-1". I only want to change selected option. And after refresh the page to the previous item was selected – Kseniia Mar 21 '14 at 10:16
  • now from click I want – Kseniia Mar 21 '14 at 10:18
  • 1
    the button in my demo only meant to trigger an alert to show you the selected value. the `select` element automatically changes the selected when you choose an option. you dont need to set anything, its being done for you by javascript automatically. – Banana Mar 21 '14 at 10:18
  • I understand it. But I must change my html code. And when I will open my page again, the selected option will be option, wich I chosen in last time. – Kseniia Mar 21 '14 at 11:21
  • i understand now, so you want to save what the user selected and pt it back when you reload the page? if thats the case you can simply add value attribute to your options and set `mySelect.value='3'` – Banana Mar 21 '14 at 11:37
  • I edited my code in question. My proиlem is not working event click. Event "change" is not working too. I think my function can change selected option, but event "click" on my options does not work! – Kseniia Mar 21 '14 at 12:16
  • depends on where you put it, javascript code can be executed before your html is loaded into the browser. therefore it will try to attach the event listener to an element that was not loaded yet. you need to put your javascript code in a code that loads **after** the html. i will update the answer; – Banana Mar 21 '14 at 12:20
  • I use document.addEventListener('DOMContentLoaded', function (){ ... }); I use my code in my chrome plugin. So it's not a web page. May be it's important in my problem. – Kseniia Mar 21 '14 at 12:24
  • check your chrome's developer console (press F12) and see if it throws any errors like "cant attach event to undefined" or something. – Banana Mar 21 '14 at 12:26
  • All is ok. When I do this with div or select it works, but with options - no :( Thank you for your time! I will try to solve this problem myself. – Kseniia Mar 21 '14 at 12:30