Cut text from an input stored in a variable with javascript or jquery

2

I am storing in a variable the .text of a input . For example the text variable:

texto = "Esto-es un mensaje"

I want to save part of the content of that text in another variable. So where is the script that saves for example the following in another variable.

texto2 = "es un mensaje"

It is important to mention that this text will be variable so it should be taken only what is after the script.

    
asked by Kinafune 01.09.2018 в 21:02
source

1 answer

4

You can use the split () function to separate the words you need, for example:

$("#Dividir").click(function () {
     var InputInicial = $("#InputInicial").val();
     var Dividido = InputInicial.split('-');
     $("#ValorAntesDeGuion").val(Dividido[0]);
     $("#ValorDespuesDeGuion").val(Dividido[1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"value="Esto-es un mensaje" id="InputInicial" />
<input type="text" id="ValorAntesDeGuion" />
<input type="text" id="ValorDespuesDeGuion" />
<button id="Dividir">dividir</button>

The number of elements of the array that generates this function depends on the number of dashes that your chain has.

Here I leave the documentation.

    
answered by 01.09.2018 / 21:19
source