Javascript concatenated in input name

2

Is there any way to concatenate a variable javascript next to a input ?

This input hidden I would submit next to an HTML form

As for example:

<script type="text/javascript">
    var strConcatenada = "STR_VARIAVEL.";
</script>

<input type="hidden" name="<%=strConcatenada%>W_TESTE_AUTO" value="1">
    
asked by anonymous 16.08.2017 / 20:41

2 answers

2

You first have to create an ID for your input, assign it to a variable:

var element = document.getElementById("inputConcatenar");

Then you give name a new value, in which case I've used the Replace that will look for a string that you pass and replace with another.

element.name = element.name.replace("strConcatenada", strConcatenada)
console.log(element.name);

I made it as simple as possible, the result is this, it follows the console.log of name. If you want to remove the "<%% >" just pass <% strConcatenada% > to Replace, thus:

replace("<%strConcatenada%>", strConcatenada)

<input id="inputConcatenar" type="hidden" name="<%=strConcatenada%>W_TESTE_AUTO" value="1">

<script type="text/javascript">
    var strConcatenada = "STR_VARIAVEL.";
    var element = document.getElementById("inputConcatenar");
    element.name = element.name.replace("strConcatenada", strConcatenada)
    console.log(element.name);
    
</script>
    
16.08.2017 / 21:00
1

You can run a javascript code to change the name when you load the page:

    <form onsubmit="myFunction()">
      <input type="hidden" name="_TESTE_AUTO" ID="_TESTE_AUTO" value="1">
      <input type="submit">
    </form>
<script>
   var strConcatenada = "STR_VARIAVEL.";
   var input = document.getElementById("_TESTE_AUTO");
   input.name = strConcatenada + input.name;
</script>
    
16.08.2017 / 21:09