0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【メモ】JQueryを使わず独自UIでSELECTタグを制御する

0
Last updated at Posted at 2019-09-13

デザイン的にSELECTタグだとどうしようもない時があるよねえ・・・。

sample.haml
  %ul#js-langList
    %li{class: ''} EN
    %li{class: ''} JA
    %li{class: ''} ZH
    %li{class: ''} FR
  %select#js-langSelector
    %option{value: 'en'} EN
    %option{value: 'ja'} JA
    %option{value: 'zh'} ZH
    %option{value: 'fr'} FR
sample.js
  document.getElementById('js-langList').addEventListener('click', function(e){
    document.getElementById('js-langSelector').options[Array.prototype.indexOf.call(e.target.parentNode.querySelectorAll("li"), e.target)].selected = true;
  }, false);

js-langSelectorはdisplay:noneして、あとは好きにスタイルを当てて下さいまし。

追記

UIの選択要素にCLASS(active)を付与する版

sample2.js
  document.getElementById('js-langList').addEventListener('click', function(e){
    var lists =  e.target.parentNode.children;
    for(var i = 0; i < lists.length; i++){
      lists[i].classList.remove('active');
    }
    e.target.classList.add('active');
    document.getElementById('js-langSelector').options[Array.prototype.indexOf.call(e.target.parentNode.querySelectorAll("li"), e.target)].selected = true;
  }, false);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?