Swap Selected of an option that is in copy script

Asked

Viewed 7,415 times

0

I’m new in the area and I’m having a hard time doing the following:

  1. I made a javascript script to create several copies of mine option. So far so good: copies made successfully.
  2. I need to make a for in javascript to go through each option copied and left on display in the first line a different sentence for each option.

I thought the following:

  • for running through the option
  • Looking for the x phrase in a option whichever
  • Did you find phrase x in option: adds a selected, so that the phrase appears as the first option of option.

Below is the test code of an html with the copy function, for a better understanding:

<!-- ... <html>, <head>, jQuery, etc ... -->
<table cellpadding="10" cellspacing="10" border="1">
    <tr>
        <td>Lixo01</td>
        <td class="Original" id="Linha1">
            <select id="MaxAlarme3">
                <option value="02">Teste1</option>
                <option value="03">Teste2</option>
                <option value="0">Teste3</option>
                <option value="04">Teste4</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Lixo02 teste</td>
        <td class="Clonar" id="Linha2"></td>
    </tr>
    <tr>
        <td>Lixo03 value 05</td>
        <td class="Clonar" id="Linha3"></td>
    </tr>
    <tr>
        <td>Lixo04</td>
        <td class="Clonar" id="Linha4"></td>
    </tr>
    <tr>
        <td>Lixo05</td>
        <td class="Clonar" id="Linha5"></td>
    </tr>
    <tr>
        <td>Lixo06</td>
        <td class="Clonar" id="Linha6"></td>
    </tr>
</table>
<script>
    //variavel contar o tamanho das copias
    $(document).ready(function() {
        //Copia valor da Linha1.Classe
        var Copiar = $("#Linha1.Original").html();
        //Clona o valor da Linha 1 para a Classe Clonar
        $(".Clonar").append(Copiar);
    });
</script>
  • Post the code where you tried to do it.

  • I don’t understand... you just want to set the OPTION that will be selected in SELECT?

  • Good morning. Silvio Andorinha, so I can’t post the code to the company :(

  • Good morning Daniel T. Sobrosa.I want each copied option to start with a different sentence. I will post a test html so that you can understand better, as I cannot post the original code.

  • @user5946, when putting some code, select it and click on the icon { } to format it.

  • Why I like to post the code on Jsfiddle: first thing I do is press Tidyup.

  • A thanks centuries light dps, I managed to post html

Show 2 more comments

4 answers

1

HTML:

<select id="opcoes">
    <option>Selecione</option>
    <option>Lorem ipsum Cupidatat sunt exercitation fugiat Excepteur.</option>
    <option>Lorem ipsum Elit sint velit nostrud minim quis.</option>
    <option>Lorem ipsum Id cillum est dolor.</option>
    <option>Lorem ipsum Irure Ut veniam cupidatat dolor culpa.</option>
    <option>Lorem ipsum Pariatur et Duis cupidatat.</option>
    <option>Lorem ipsum Esse laboris veniam irure eu irure.</option>
    <option>Lorem ipsum Anim eu culpa magna quis irure.</option>
    <option>Lorem ipsum Dolore Excepteur esse aliquip Duis eu.</option>
    <option>Lorem ipsum Duis ut velit non.</option>
</select>

Javascript:

$(document).ready(function() {
    // frase que desejo localizar
    var frase      = "Lorem ipsum Dolore Excepteur esse aliquip Duis eu.",
        localizado = null;

    // loop que percorre cada uma das opções
    // e verifica se a frase da opção confere com o
    // valor de fase que está sendo procurado
    $('#opcoes option').each(function() {
      // se localizar a frase, define o atributo selected
      if($(this).text() == frase) {
        $(this).attr('selected', true);
      }
    });
});
  • Oiii found out what I was missing your code this right. Now I will try to adapt mine. Thank you

  • Another thing if possible I need to call the phrases by value. Can you send me an example? Thank you.

0


I got what I wanted.

// frase que desejo localizar
    var frase      = "04";//eu queria que a variavel frase pegasse o value dos options e de acordo com o valor do
    //option eu poderia escolher a frase que iria iniciar o mesmo
       var localizado = null;

    // loop que percorre cada uma das opções
    // e verifica se a frase da opção confere com o
    // valor de fase que está sendo procurado
    $('#Linha6 option').each(function() {
      // se localizar a frase, define o atributo selected
      if($(this).attr('value') == frase) {//-----Alterei de $(this).value() para o attr.()-----
        $(this).prop('selected', true); //-----Pode-se utilizar .attr('selected', true) também-----
      }
    });

Thank you all for your help.

0

Well, one way to keep your code cleaner would be by using the $.find method below:

HTML

<select id="opcoes">
  <option value="0">Selecione</option>
  <option value="1">Lorem ipsum Cupidatat sunt exercitation fugiat Excepteur.</option>
  <option value="2">Lorem ipsum Elit sint velit nostrud minim quis.</option>
  <option value="3">Lorem ipsum Id cillum est dolor.</option>
  <option value="4">Lorem ipsum Irure Ut veniam cupidatat dolor culpa.</option>
  <option value="5">Lorem ipsum Pariatur et Duis cupidatat.</option>
  <option value="6">Lorem ipsum Esse laboris veniam irure eu irure.</option>
  <option value="7">Lorem ipsum Anim eu culpa magna quis irure.</option>
  <option value="8">Lorem ipsum Dolore Excepteur esse aliquip Duis eu.</option>
  <option value="9">Lorem ipsum Duis ut velit non.</option>
</select>

Javascript

$(document).ready(function() {
  // valor que desejo localizar
  var value = "1";

  // Usa-se o método $.find para buscar a
  // opção pelo atributo value e o seleciona
  $('#opcoes').find('[value="' + value + '"]').attr('selected', true);
});
  • Okay thanks, as soon as possible I’ll test and I’ll tell you

0

No need to go through the select, with a simple line you can do this!

HTML

<select id="cores">
    <option>Selecione</option>
    <option value='1'>Vermelho</option>
    <option value='2'>Azul</option>
    <option value='3'>Verde</option>
</select>

Jquery (Select blue color)

$("#cores>option[value=2]").attr("selected", true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.