How to use the value of options?

4

In the "options.html" file I know exactly how to make a markup work to enable some javascript set like ...

<label>
    <input type="checkbox" id="test">
    Habilitar
</label>

That will execute the following codes inside options.js:

var test = document.getElementById('test').checked;

test: test

test: false

document.getElementById('test').checked = items.test;

Then the final result will be activated within an exemple.js:

var storage = chrome.storage.sync;
var info = 0;

var total_requests = 0;
var processed_requests = 0;

var cookie = document.cookie;

storage.get(function(settings) {
    if (settings.test === undefined) { 
        settings.test = false;
        storage.set({'test': settings.test}); 
    }
    if (settings.test) {
        \alguma coisa aqui
    }
});

If you understood what I meant, now comes my doubt; I'm wondering how to do this with some value of a setting being set to a <select> .

<select id="exemplo">
    <option value="exemplofoi">Padrão</option>
    <option value="exemplofoi2">Extra</option>
</select>

If you have not understood, I'm looking to use the storage.get to activate a javascript only when this value is active on the extension settings page.

Answer:

storage.get(function(settings) {
    if (settings.exemplo === undefined) { 
        settings.exemplo = false;
        storage.set({'exemplo': settings.exemplo}); 
    }
    if (settings.exemplo == 'exemplofoi') {
        \alguma coisa aqui
    }
});
    
asked by anonymous 27.08.2015 / 05:04

2 answers

2

If I get it right, you want to get the value of < option > that is selected, I will respond with this understanding:

<!DOCTYPE html>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body><selectid="exemplo">
        <option value="option01">Padrão</option>
        <option value="option02">Opção 2</option>    
    </select>

    <button id="pegar">Pegar Valor</button>
    
<script>
    $("#pegar").click(function(){ 
        var opcao = $('#exemplo :checked').val()
        if( opcao == 'option01' ){
            //chama sua funcao aqui
            alert( 'chama sua funcao na opcao1' );
        }
        if( opcao == 'option02' ){
            //chama sua funcao aqui
            alert( 'chama sua funcao na opcao2' );
        }
            
    });
</script>

</body>
</html>
    
27.08.2015 / 13:02
2

To get the value of option you just get the value of select , after all the one who receives the name attribute is it, that is, when you select some option you are setting the select will be the value of that option .

As a code speaks more than a thousand words, here's an example using just Javascript :

var select = document.getElementById('select');

console.log(select.value);

var button = document.getElementById('button');

button.addEventListener('click', function(event){
  alert(select.value);
});
<select id="select">
  <option value="1">Um</option>
  <option value="2">Dois</option>
  <option value="3">Três</option>
</select>

<button id="button">Ver</button>

Here's an example using jQuery :

jQuery(document).ready(function($){


  var $select = $('#select');
  
  var $button = $('#button');
  
  $button.click(function(event){
    alert($select.val());
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><selectid="select">
  <option value="1">Um</option>
  <option value="2">Dois</option>
  <option value="3">Três</option>
</select>

<button id="button">Ver</button>
    
27.08.2015 / 13:33