javascript - How do you set attribute selected for element and remove attributes from rest? -


The task I am trying to accomplish is when the user chooses the option, then select it "selected" Remove the "selected" attribute for the element and the other elements listed.

  & lt; Id = "tseelect" class = "form-control input-sm" & gt; & Lt; Option value = "approach" & gt; Outlook & lt; / Options & gt; & Lt; Option value = "metallium" & gt; Metallium & lt; / Option & gt; & Lt; Option value = "osm" & gt; Osat & lt; / Option & gt; & Lt; / Select & gt;  
  $ ("# tSelect"). Change (function () {$ ("# t selection option: selected"). RemoveAttr ('selected'); var val = $ (this) .val (); $ ('# tSelect option [value =' '+ val + '"]'). Et. (" Selected "," Selected ");});  

This is my code so far, but it is not working.

Now, why do you need it when I get it, is possible solution, note that X-selected requires shenanigans with custom attribute because you can not set / delete the selected attribute without interfering with the actual option selection. You can set the dummy attribute and when you choose HTML then replace it with the actual.

  $ (" # tSelect "). Change (function () {$ (this.options) .removeAttr ('X-selected'); $ (this) .find (': selected'). Attr ('x-selected', 'selected'); ou External Html = this.outerHTML.replace (/ x-selected / g, 'selected'); Alert (external HTML);));  
  & lt; Script src = "https: //ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> & Lt; Id = "tseelect" class = "form-control input-sm" & gt; & Lt; Option value = "approach" & gt; Outlook & lt; / Options & gt; & Lt; Option value = "metallium" & gt; Metallium & lt; / Option & gt; & Lt; Option value = "osm" & gt; Osat & lt; / Option & gt; & Lt; / Select & gt;  


Comments