I have autocomplete nested within a jquery that creates text elements on the fly. The autocomplete works properly on the #url text field on every text field I create, however, I cannot make it work on the price field.
It only works if nextUrlId is not increasing every time I press "Add" (without nextUrlId++). Because of this, I cannot autopopulate the price field upon selection from autocomplete.
This does not work:
$('#AddUrl').click(function(){
$('<p />').attr('id', 'urlParagraph' + nextUrlId)
.text("URL: ")
.appendTo('#inputBoxes');
$( "#url"+nextUrlId ).catcomplete({
delay: 0,
minLength : 2,
source: "searchsku.php",
select: function(event, ui){
$("#price"+nextUrlId).val(ui.item.price)}
});
NextUrlId++
HTML:
<div id="inputBoxes">
<table border="1">
<tr>
<td><p id="nameParagraph">Name: <input type="text" id="name" /></p> </td>
</tr>
<tr>
<td><p id="urlParagraph1">URL: <input type="text" id="url1" /></p> </td>
</tr>
</table>
</div>
If I get it right, you have two related fields for each URL. One with an autocomplete box, and one with a price. Depending on the autocomplete entry, the price box should change value. I haven't tested this, but I belive something like this should work well. Instead of keeping track of ID's, you're just looking at the sibling element of the autocomplete field. Many ways of doing this, two examples below, in preferred order.
<div id="inputBoxes">
<p class="inputbox">
<input type="text" class="autocompleter" name="autocompleter[]">
<input type="text" name="price[]" class="price">
</p>
<input type="button" value="Add" id="addURL">
</div>
Solution 1
$(".inputbox").find(".autocompleter").catcomplete({
delay: 0,
minLength : 2,
source: "searchsku.php",
select: function(event, ui){
$(this).siblings(".price").val(ui.item.price);
}
});
$("#addURL").bind("click",function(){
var last = $(".inputbox").last();
var newItem = last.clone()
newItem.insertAfter(last);
newItem.find(".autocompleter").catcomplete({
delay: 0,
minLength : 2,
source: "searchsku.php",
select: function(event, ui){
$(this).siblings(".price").val(ui.item.price);
}
});
});
Solution 2
If you would prefer a fixed relation between the autocomplete field and the price fields within each paragraph, I'd do something like this.
<div id="inputBoxes">
<p class="inputbox">
<input type="text" rel="1" class="autocompleter" name="autocompleter[]">
<input type="text" rel="1" name="price[]" class="price">
</p>
<input type="button" value="Add" id="addURL">
</div>
$(".inputbox").find(".autocompleter").catcomplete({
delay: 0,
minLength : 2,
source: "searchsku.php",
select: function(event, ui){
$('.price[rel="'+$(this).attr("rel")+'"]').val(ui.item.price);
}
});
$("#addURL").bind("click",function(){
var last = $(".inputbox").last();
var newItem = last.clone()
newItem.insertAfter(last);
var lastRel = last.find(".autocompleter").attr("rel");
newItem.find(".autocompleter").attr("rel",lastRel+1);
newItem.find(".price").attr("rel",lastRel+1);
newItem.find(".autocompleter").catcomplete({
delay: 0,
minLength : 2,
source: "searchsku.php",
select: function(event, ui){
$('.price[rel="'+$(this).attr("rel")+'"]').val(ui.item.price);
}
});
});
Related
I am using JQuery autocomplete option to get values from database using PHP. I have 2 text boxes and I am trying to use same PHP file to get the required results.
My HTML code is
<tr>
<td>LOB</td>
<td><input autocomplete="off" type="text" name="ABC" class="autocomplete"/></td>
</tr>
<tr>
<td>Project Name</td>
<td><input autocomplete="off" type="text" name="PQR" class="autocomplete"/></td>
</tr>
and my JQuery is
$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $(".autocomplete").attr('name') + '&',
minLength: 2
});
});
But as these there are 2 elements with same class. I am not getting correct arrr('name'). MySQL will differ based on this name. I tried (this) but still not getting the result.
If I am in the first text box then name should be of that text box etc..
Please let me know if I am missing anything.
You can target the textbox using name.
$('[name=ABC]').val()
Use id to differentiate
<tr>
<td>LOB</td>
<td><input autocomplete="off" type="text" name="ABC" id="id1" class="autocomplete"/></td>
</tr>
<tr>
<td>Project Name</td>
<td><input autocomplete="off" type="text" name="PQR" id="id2" class="autocomplete"/></td>
</tr>
//Script
$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $("#id1").attr('name') + '&',
minLength: 2
});
});
You're mixing the scope of 'all autocompletes' and 'the current automplete'. One way of dealing with this is to use jQuery's each() method:
$('.various-things').each(function () {
console.log($(this).attr('something');
});
This would log the 'something' attribute of each .various-things on the page. It looks like what you'd need is:
$(document).ready(function() {
$( ".autocomplete" ).each(function () {
$(this).autocomplete({
source: 'LiveSearch.php?name='+ $(this).attr('name') + '&',
minLength: 2
});
});
});
Now it'll find all autocompletes and set them up using their own name.
I have a problem. jQuery Datepicker does not work in a dynamic form, so you can't pick a date. Here is my demo link http://gestionale.odoyabooks.com/sum.php.
JavaScript:
<script>
$(document).ready(function() {
$('.input_date').on('click', function() {
$(this).datepicker('destroy').datepicker({showOn:'focus'}).focus();
});
});
</script>
HTML:
<form action="" method="POST">
<div class="yes">
<div id="cost1" class="clonedCost" style="display: inline;">
<table border="0">
<tr>
<td><label class="date" for="date">Date</label></td>
</tr>
<tr>
<td><input class="input_date" id="date" type="text" name="date[]" /></td>
</tr>
</table>
</div>
<div id="addDelButtons_cost"><input type="button" id="btnAdd" value=""> <input type="button" id="btnDel" value=""></div>
</div>
</form>
The problem is that when you bind the event initially, the new field doesn't exist.
By applying the on click to the body, with the selector .input_date, it should attach the event to the new element in the dynamic form.
$(function() {
$('body').on('click','.input_date', function() {
$(this).datepicker('destroy').datepicker({showOn:'focus'}).focus();
});
});
Working Example
A better way:
You should initialize the datepicker for the field when you create the element.
$(function() {
$('.input_date').datepicker();
});
function createNewDatepickerInput() {
var $newInput = $('<input type="text" name="date123" class="input_date" />');
$newInput.appendTo("form").datepicker();
}
Working Example
I don't understand why you're destroying/re-initializing the datepicker.
You simply need to call .datepicker() after creating the element. For example:
function createInput() {
$('<input type="text" name="date" />').appendTo("form").datepicker();
}
Edit:
Here's a demo: http://jsfiddle.net/xEmJu/
I am trying to call the autocomplete function on all textboxes with class='matricula' so that when they lose focus, the boxes to their right will be populated with data coming from the same record (the query that I use to do this is found in the php files buscaralumno.php and alumno.php).
The thing is, the way it is right now when the cursor leaves the first textbox in the first row, it populates ALL the remaining textboxes - not only the first row.
So the bottom line would be: when I enter at least 2 letters in the first textbox in each row, a list suggesting possible values should pop up (which is what it does right now) and when I press the tab key to change focus to the next textbox, the remaining textboxes should be populated with the values of the remaining fields. In other words, one row of textboxes corresponds to a record from the database.
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<link href="css/jqueryui.css" type="text/css" rel="stylesheet"/>
<script>
$(document).ready(function(){
$( ".matricula" ).autocomplete({
source: "buscaralumno.php",
position: { my: "right bottom", at: "right top", collision: "flip" },
messages: { noResults: '', results: '' },
minLength: 2
});
$(".matricula").focusout(function(){
$.ajax({
url:'alumno.php',
type:'POST',
dataType:'json',
data:{ matricula:$('.matricula').val()}
}).done(function(respuesta){
$(".nombre").val(respuesta.nombre);
$(".paterno").val(respuesta.paterno);
$(".materno").val(respuesta.materno);
});
});
});
</script>
</head>
<form>
<label for="matricula">Matricula:</label>
<input type="text" class="matricula" name="matricula" value=""/>
<label for="nombre">Nombre:</label>
<input type="text" class="nombre" name="nombre" value=""/>
<label for="paterno">Paterno:</label>
<input type="text" class="paterno" name="paterno" value=""/>
<label for="materno">Materno:</label>
<input type="text" class="materno" name="materno" value=""/><br>
<label for="matricula">Matricula:</label>
<input type="text" class="matricula" name="matricula" value=""/>
<label for="nombre">Nombre:</label>
<input type="text" class="nombre" name="nombre" value=""/>
<label for="paterno">Paterno:</label>
<input type="text" class="paterno" name="paterno" value=""/>
<label for="materno">Materno:</label>
<input type="text" class="materno" name="materno" value=""/><br>
</form>
Any hints will be more than appreciated. I apologize if this question is poorly phrased. I am fairly new to jQuery and English is not my first language.
Try
$(".matricula").focusout(function(){
var $row = $(this), inputs = $row.nextUntil('.matricula', 'input');
$.ajax({
url:'alumno.php',
type:'POST',
dataType:'json',
data:{ matricula:$('.matricula').val()}
}).done(function(respuesta){
inputs.filter(".nombre").val(respuesta.nombre);
inputs.filter(".paterno").val(respuesta.paterno);
inputs.filter(".materno").val(respuesta.materno);
});
});
I have multiple autocomplete textbox paired with multiple hidden fields. How do I do that? Ex. textbox1:name = hiddenfield1: Id, textbox2:name = hiddenfield2: Id.
I was already able to make 1-autocomplete and 1-hiddenfield work.
Here is the code for my script:
<script type="text/javascript">
$(document).ready(function() {
$('.auto').autocomplete({
source: "search.php",
focus: function(event, ui) {
$(idField).val(ui.item.value);
$(this).val(ui.item.label);
return false;
},
select: function(event, ui) {
$(this).val(ui.item.label);
$("#hidden").val(ui.item.value);
return false;
}
//minLength: 3
});
});
</script>
<p>Type the name of a band: <input type="text" class="auto" /></p>
<p>Type the name of a band: <input type="text" class="auto" /></p>
<input name="hidden" id="hidden" type="hidden" />
<input name="hidden" id="hidden" type="hidden" />
Sir/Ma'am your answers would be of great help and very much appreciated.
Firstly, you'd need unique identifiers for all of your input fields, hidden or not. Then assigning values to them would be a lot easier. You're really close, and I'd only change a few things to get it working, mostly to do with the IDs of the elements you're using:
<script type="text/javascript">
$(document).ready(function() {
$('.auto').autocomplete({
source: "search.php",
...
select: function(event, ui) {
// figure out which auto we're using and get it's
// associated hidden field...
var element_id = $(this).attr('id');
var hidden_element_id = element_id + "_hidden";
// set the appropriate fields' values...
$(this).val(ui.item.label);
$("#"+hidden_element_id).val(ui.item.value);
return false;
}
...
});
});
</script>
<p>Type the name of a band: <input type="text" class="auto" id="auto1" /></p>
<p>Type the name of a band: <input type="text" class="auto" id="auto2" /></p>
<input name="hidden" id="auto1_hidden" type="hidden" />
<input name="hidden" id="auto2_hidden" type="hidden" />
One of the easier ways to associate hidden fields with visible counterparts...you get the ID of the element that's currently being autofilled, then grab its hidden field counterpart by just appending '_hidden' onto its ID attribute...make sense?
Don't forget to change the ID attributes of the fields! Hope this helps!
I'm creating a PHP site for property. I'm new to jQuery and jQuery UI but can't seem to find the answer anywhere else.
Please see this screen shot (full size):
For for every "received" and "expiry" box I want a jQuery datePicker box to appear and format as shown.
To test this I tried to create an example.
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.9.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
$( "#datepicker2" ).datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
<style>
.ui-datepicker {
font-size: 80%;
}
</style>
<p>Date: <input id="datepicker" type="text" /></p>
<p>Date2: <input id="datepicker2" type="text" /></p>
Which works fine, but how do I get a date picker to come up for ANY textbox without having to repeat the JS so many times.
For say, 50 properties, there may be 200 input boxes on the page with a date needing to be filled in.
Here is some example code from the screen shot.
<tr>
<td>Property ID</td>
<td>House Number</td>
<td>Address Line 1</td>
<td>Gas Expiry</td>
<td>Gas Received</td>
<td>Electric Expiry</td>
<td>Electric Received</td>
<td>Property Active</td>
<td>Update</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
<td>East Street</td>
<td><input name="gas_expiry[4]" type="text" id="gas_expiry[4]" value="2011-08-03" /></td>
<td><input name="gas_received[4]" type="text" id="gas_received[4]" value="" /></td>
<td><input name="electric_expiry[4]" type="text" id="electric_expiry[4]" value="" /></td>
<td><input name="electric_received[4]" type="text" id="electric_received[4]" value="" /></td>
<td>
<select name="active[4]" id="active[4]">
<option value="0" selected="selected">No</option>
<option value="1">Yes</option>
</select>
</td>
<td><input name="edit[]" type="checkbox" id="edit[]" value="4" /></td>
</tr>
Probably something really simple, I appreciate any help.
What I do is create a calendar CSS class and have the behavior attached to each member of that CSS class like so:
$( ".calendar" ).datepicker({ dateFormat: 'yy-mm-dd' });
You should set up class attribute for each text box
<input class="datepicker" type="text" />
and then
$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
Or if you have only dates textboxes, you can attach datepicker for all textboxes by
$( 'input[type="text"]' ).datepicker({ dateFormat: 'yy-mm-dd' });
Another method, just for your reference, would be
$("#datepicker, #datepicker2").datepicker({ dateFormat: 'yy-mm-dd' });
For ANY textbox on the page:
$('input[type="text"]').datepicker({ dateFormat: 'yy-mm-dd' });
to get functional datepickers on different input boxes on the same page of a custom post with the least hassle and without messing with the jquery-ui.css and jquery-ui.theme.css or any other code of the jQuery ui library:
<input class="datepicker" name="first_intake" id="first_intake" value="" size="30" type="date">
<input class="datepicker" name="second_intake" id="first_intake" value="" size="30" type="date">
In the footer I initiate the datepicker in a function as I use wordpress and this page is in the admin backend, but you can just use the javascript snippet, if you do not use wordpress:
<?php
function my_admin_footer() { ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#first_intake.datepicker').datepicker({
dateFormat : 'd M, yy'
});
jQuery('#second_intake.datepicker').datepicker({
dateFormat : 'd M, yy'
});
});
</script>
<?php
}
add_action('admin_footer', 'my_admin_footer');
?>
Here's what worked for me...
PHP + jquery
$(function() {
for(let i = 1; i<= <?php echo count($rows) ?>; i++)
{
$( "#datepicker-popup"+i ).datepicker({ dateFormat: 'yy-mm-dd' });
}
});