How to send value from a div to an input

0

I am working with datapicker bootstrap in the range of dates and particularly this container has no input to select the date, rather it is based on a div

 <div id="reportrange" name="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
        <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
        <span></span> <b class="caret"></b>
</div>

which dates can not be sent by post or by get I do not understand why, try to create a hidden input to pass the values through a onchange function but it does not work either! ideas ....?

$('#reportrange').on('change',function myFunction(){
var x = $('#reportrange').val();
$("#oculto").val(x);
});

where #aculto is my input

    
asked by matteo 07.06.2017 в 21:19
source

3 answers

1

The solution to the problem was quite simple,

I first realized that the date range of the div container is generated in the span at run time, so I chose to create a function in javascript that would get that text.

<script>
  $(document).ready(function(){
    $("#btnrango").click(function(){
      var fechas_rango = $("#texto").text();
      alert(fechas_rango);
    });
  });
</script>

After that I can already have the range of dates within a variable var dates_range to send it by get or whatever I can think of.

    
answered by 08.06.2017 / 18:46
source
3

I'll give you an example with a daterange also with bootsrap I hope it works for you.

$('input[name="daterange"]').daterangepicker(
{
    locale: {
      format: 'YYYY-MM-DD'
    },
    startDate: '2013-01-01',
endDate: '2013-12-31'
}, 
function(start, end, label) {
    alert("El Rango de fecha : " + start.format('YYYY-MM-DD') + '  Al  ' + end.format('YYYY-MM-DD'));
});
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/3/css/bootstrap.css" />
 
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<div>
    <input class="form-control text-center" name="daterange" value="" />
</div>
<br>
    
answered by 07.06.2017 в 21:56
2

I do not know what you're trying with that code, but the div you have in first must be converted to the datepicker class so it can be used and a value returned. If you already have it, it's too easy. In the second, you are trying to trigger an event that does not correspond to the behavior of a div.

  

link

Now that if what you want is that when you click on the div an event is triggered I can help you with this code:

<script>
  $(function() {
    $("#reportrange").datepicker({
      onSelect: function(date) {
        //PUEDES HACER UN AJAX O LO QUE QUIERAS, LA VARIABLE DATE CONTIENE LO QUE ELEGISTE DEL CALENDARIO
      }
    });
  }); 
</script>
    
answered by 07.06.2017 в 22:38