datePicker with concatenated name

0

I need to call the datePicker function by concatenating a fixed name with the name I get from a query. It's possible?

I tried this way:

$("validade_T'.$v_id'").datepicker({

Where validade_T is the fixed value and $v_id would be the value that comes from the bank. That way you returned the error inside jQuery.js:

  

Error: Syntax error, unrecognized expression: validity_T '. $ v_id'

     

throw new Error ("Syntax error, unrecognized expression:" + msg);

    
asked by anonymous 16.04.2018 / 16:02

1 answer

1

Try this:

var v_id = <?php echo $v_id; ?>; //criar uma variável para receber esse valor vindo do PHP
$('validade_T'+v_id).datepicker({  //concatenar a variável criada

Here's how to concatenate a variable in jQuery :

$(document).ready(function() {
  var id = 12; //aqui você só associaria o valor recebido da consulta (<?php echo $v_id; ?>)
  $('#data'+id).change(function() {
    var data = $('#data'+id).datepicker('getDate');
    $("#dataEscolhida").html(data);
  });
  $(function() {
      $('#data'+id).datepicker({dateFormat: 'dd/mm/yy'});
  });	
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Data: <input type="text" id="data12"></p>

<b>Data escolhida: </b><div id="dataEscolhida"></div>
    
16.04.2018 / 16:25