Pass jQuery variable value to an input

1

I have this function in jQuery

<script type="text/javascript">
    $(document).on("ready", function () {
        $("#area_tabla table tr td").click(function () {
            var celda = $(this);
            alert(celda.html());
            console.log(celda.html())

        });
    });
</script>

What it does is that when selecting a cell in a table it shows me the value of that cell in a alert . What I want is for that value to be assigned to <input>

<input type="text"/>
    
asked by Jesus Galeano 07.12.2016 в 18:06
source

2 answers

1

You can get it by using the .prop () method to pass the value:

var celda = 'algo';

$('input[type=text]').prop({'value': celda});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<input type="text">
    
answered by 07.12.2016 / 18:11
source
1

Assuming your <input> is declared like this:

<input type="text" id="txtCeldaModificar"/>

Then your jQuery code will be as follows:

<script type="text/javascript">
    $(document).on("ready", function () {
        $("#area_tabla table tr td").click(function () {
            var celda = $(this);
            $("#txtCeldaModificar").val(celda.html());
        });
    });
</script>
    
answered by 07.12.2016 в 18:23