How to change an event in jquery? [closed]

-2

I have a modal which has two functions, to record the data received by ajax in tags <span class="nombre"></span> and the other alternative is to be able to give the user the option that when he presses a button that data becomes a form ...

the function is the following:
 to show the data without editing use $('.nombre').html(data[0].nombre);

but I want that when you press the "editar" button change that $('.nombre').html(data[0].nombre); for the $('.nombre').val(data[0].nombre); , with what purpose? so that you do not have to return to make an ajax call to re-format the data in the inputs of the form to be edited.

since if I use $('.nombre').html(data[0].nombre); , that data will not be shown in the input of the form.

I hope you have made me understand and I would appreciate the interest

    
asked by JDavid 30.03.2017 в 15:33
source

1 answer

1

Well, the simplest thing, instead of changing the event according to the situation, can be sending the values to the corresponding element in the corresponding way.

For example:

in the case of having these two elements:

<input class="nombre" /> <span class="nombre"></span>

You can specify through the selector which ones you want to pass the value in the appropriate way.

$('input.nombre').val('nombre');
$('span.nombre').html('nombre');

This will allow you to execute the same code regardless of whether the elements are visible or not.

setTimeout(function() {
  $('input.nombre').val('nombre');
  $('span.nombre').html('nombre');
}, 1500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="nombre" /> <span class="nombre"></span>
    
answered by 30.03.2017 / 17:28
source