Get input date value

Asked

Viewed 1,948 times

-1

I have two input date created, and would like to take the date of the two by a button.

            <div class='col-md-5 col-lg-4'>
            <div class="form-group">
                <span>Inicio</span>
                        <div class='input-group date' id='datetimepicker6'><span></span>
                                <input type='text' class="form-control" />
                                <span class="input-group-addon">
                                        <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                        </div>
                </div>
    </div>
    <!-- -->
    <div class='col-md-5 col-lg-4'>
        <div class="form-group">
                <span>Fim</span>
            <div class='input-group date' id='datetimepicker7'>
                <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
    <!-- -->
    <div class="col-md-5 col-lg-3">
        <div id="espaco"></div>
            <button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off" onclick = "botaoFunfa>
              Buscar
            </button>
    </div>

Every time I try to catch it shows the code and not the value

JS

function botaoFunfa() {
    var teste = document.querySelector("#datetimepicker6");
    console.log(teste);
}

I managed to get the amount as follows:

I was able to solve it this way:

function botaoFunfa() {
    var teste = document.querySelector(".switch");
    var StringData = teste.textContent || teste.innerText; 
    console.log(StringData);
}

So it shows the day, month and year. Obs.: the switch class is the bootstrap itself.

But for some reason it’s giving some bug, and it doesn’t get both input date, just one.

  • try with var teste = document.querySelector("#datetimepicker6").value;

  • I managed to solve by pulling the switch class, but it does not catch both and error

  • datetimepicker6 does not own a property value, note that the id is in a div, if you want to get the value of a single element input for his id. You should first inform the id in HTML.

1 answer

1


Follow a solution below, just search the val() from each to the button be clicked.

    
    $('#btn').click(function(){
      var inicio = $('#inicio').val(),
      fim = $('#fim').val();       
       console.log('data inicial: ' + inicio + ' data final: ' +fim);
       
    })
    
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

 <div class='col-md-5 col-lg-4'>
            <div class="form-group">
                <span>Inicio</span>
                        <div class='input-group date' ><span></span>
                                <input id='inicio' type='text' class="form-control" />
                                <span class="input-group-addon">
                                        <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                        </div>
                </div>
    </div>
    <!-- -->
    <div class='col-md-5 col-lg-4'>
        <div class="form-group">
                <span>Fim</span>
            <div class='input-group date' >
                <input id='fim' type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
    <!-- -->
    <div class="col-md-5 col-lg-3">
        <div id="espaco"></div>
            <button type="button" id="btn" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off" >
              Buscar
            </button>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.