datepicker with concatenated name

Asked

Viewed 43 times

0

I need to call the function datePicker concatenating a fixed name with the name I bring from a query. It is possible?

I tried that way:

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

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

Error: Syntax error, unrecognized Expression: validade_T'. $v_id'

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

  • It comes from a echo in php ? This $v_idcomes from where ?

  • Yes, it’s from a PHP. The $v_id comes from a search in the Oracle database.

  • are presenting the $v_id? if yes, do not need to identify it so in js.

1 answer

1


Try it like 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 is an example of how to concatenate a variable into 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>
<script src="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>

  • 1

    Exactly, that was it. Thank you.

Browser other questions tagged

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