How to put a java script inside the value html

0

I have a variable of any kind:

let variavel = x;

and I have the input:

<input type="text" value="">

Is there a way in javascript pure to pick up a variable or any javascript code and put inside html tags?

In this example it would look like this:

<input type="text" value="<script>variavel</script>">

I know this is not how it works, but has a similar way to do this with javascript? Thanks to anyone who responds

    
asked by anonymous 01.03.2018 / 22:19

2 answers

1

Set an id for your html element

<input type="text" id="idUnico">
<script>
    var variavel = x;
    document.getElementById("idUnico").value = variavel;
</script>

If you are using jquery

<script>
    var variavel = x;
    $("#idUnico").val(variavel);
</script>
    
01.03.2018 / 22:23
0

Complementing Mark's response:

For you to assign the value, you will use:

document.getElementById("idUnico").value = '<script>alert()</script>'

Now to read the value, you will use:

document.getElementById("idUnico").value

If it's a function within value, I believe you'll need to get it and inject it into HTML to make it available.

If your purpose is to do XSS, be more specific so that we can respond more effectively.

I believe this is not a good practice and can make it difficult to maintain your application.

I hope I have helped, but I believe there may be a more cohesive way to solve your problem, maybe explaining the context helps us help you.

EDIT1: The contents of the variable is a string, if you have problems with using just pass only the function or escape the characters. Notice that value stores a string, not the JS function. An example of this is that you could very well convert your function to base64 and save to variable. Next, get the string and make the decode to get the function clean.

    
01.03.2018 / 22:37