How do I troubleshoot jQuery? - jquery-ui

I am a newbie with jQuery and wondering if someone would just give me a little push in the right direction.
I am writing (more like copy-pasting) autocomplete code from other SO answers:
$("#addmore").on("click", function(){
var rownum = $('#orderTable tr:last').attr('id')*1+1;
var rowhtml =' <tr id="' + rownum +'"> '+
' <td> '+
'<input type="text" class="autoCompItem form-control input-small" id="itembox" name="item" placeholder="Eggs" value="e"> '+
' </td> '+
' <td> '+
'<input type="text" class="form-control input-small" id="quantity1" name="quantity1" placeholder="24"> '+
' </td> '+
' <td> '+
'<select class="form-control "> '+
'<option value="">Units</option> '+
'<option value="lbs">lbs</option> '+
'<option value="packs">Packs</option> '+
'<option value="cartons">Cartons</option> '+
'<option value="gallons">Gallons</option> '+
' </select> '+
' </td> '+
' <td> '+
'<input type="text" class="form-control input-small" id="brand1" name="brand1" placeholder="Eggland"> '+
'</td> '+
'<td> '+
'<input type="text" class="form-control input-small" id="comment1" name="comment1" placeholder="Choose fresh"> '+
'</td> '+
'</tr>';
$(rowhtml).appendTo("#orderTable");
$(rowhtml).find(ipt).autocomplete({
source: function(request, response){
alert('req term:'+request.term);
$.ajax({
url: '/api/autocomplete',
data: {'q': request.term},
dataType: "json",
success: function(data){
response(data);
}
});
},
focus: function(event, ui){
$(this).val(ui.item.name);
return false;
},
select: function(event, ui){
$(this).val(ui.item.name);
return false;
}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return $( "<li></li>" )
.data( "ui-autocomplete-item", item )
.append( "<a>" + item.name + "</a>" )
.appendTo( ul );
};
});
My renderItem function fails and I want to break the problem down to see if my rowhtml (a dynamically added element) variable is not finding ipt selector or whether there is another issue. Can someone guide me with this issue ... please!!

Part 1.
I would start by checking for errors in the developer console. To do this right click on the page after it has loaded and click on inspect element. After you complete that step you will see a new window on the bottom of the screen. Next select the console tab.
What I would after selecting the console tab is hit the f5 button
which will refresh the page. The advantage to this that it will see
if your code has any errors on page load.
After you have done that typing in your autocomplete text box and see if you get any errors in the console. If a variable value is not set JavaScript will output undefined to the console. If an object's value is not set JavaScript will output null.
Write down any errors that you recieve and when they occured and I will do my best to help you debug your code.
Part 2.
Now that you have discovered an error. We need to back track a step. I put the code you posted inside of a try catch block and loading it in a jsfiddle to see what errors occur on load. You should have got the same error by following the step highlighting in part 1. Unless there is some code missing from your question.
When I ran the following fiddle:
https://jsfiddle.net/larryjoelane/6hkrdrv1/7/
var rowhtml =' <tr id="' + rownum //<--- Is this variable value assigned?
I get this error message: ReferenceError: rownum is not defined. When I look at your code I notice that the rownum variables value is not set in your code. Once you have set a value for rownum. check for more errors and let me know which ones you receive.

Related

select2 multiple with ajax data initializing with Undefined

I followed the example provide in the docs, and applied it to a select multiple but some things don't work as expected.
Instead of getting the actual values I get Undefined, Undefined
Here is the JSFiddle code with the problem
Am I missing a step?
This part of the example provided here doesn't work for me. The console.log shows the correct data but select2 shows undefined in the select
jQuery.each(data, function(index, item) {
console.log(item);
var option = new Option((item.name + ' (' + item.code + ')'), item.id, true, true);
preRequisites.append(option).trigger('change');
});
preRequisites.trigger({
type: 'select2:select',
params: {
data: data
}
});
Thank you
the code needed this part item.text ||
templateSelection: function(item) {
return item.text || (item.name + ' (' + item.code + ')');
},

Url against jQuery ui autocomplete selected value

$( function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#searchcomplete" ).autocomplete({
source: "<?php echo base_url('home/search');?>",
minLength: 2,
select: function( event, ui ) {
log( "Selected: " + ui.item.value + " aka " + ui.item.id );
}
});
} );
I don't know how to set a url against the user's selected id in this widget. What i want here is when user select any option from dropdown and click on it then page will be redirected to url against that selected id.
I solved it using form.
<form id="tpsearch" action="" method="post">
<input type="text" id="searchcomplete">
<button type="submit" class="icon-search"></button>
</form>
and select function will be from autocomplete:
select: function( event, ui ) {
log( "Selected: " + ui.item.value + " aka " + ui.item.id );
$("#tpsearch").attr("action", "<?php echo base_url(); ?>/controller/action/" + ui.item.id);
}

jquery mobile data-lastval

I need help with an Intel XDK project.
After research I was succesfull to create an autocomplete form.
However, if the user do not select the value from filter list,
and enter the value manually, it is not saved to the variable.
It would be possible to select the value in "data-lastval".
Here is the code that appears in DOM. But with all the research I do, I cannot understand, how to fetch this content of data-lastval and put in into a variable. On selection of the list item it goes saved to a hidden input field and can be stored to localstorage. I need help in building this javascript or jquery mobile selector, like var xstreet = ...
"If"-logic will decide, that the hidden field was empty, and then put "xstreet" instead in this.
<div class="ui-input-search ui-shadow-inset ui-input-has-clear ui-body-e ui-corner-all">
<input data-type="search" placeholder="street" data-lastval="2323">
Clear text</div>
Code in src:
<div class="widget uib_w_15 form1widths labelwhite form1forms streetfield" data-uib="jquery_mobile/input" data-ver="0" id="streetfield"><input type="hidden" id="xd">
<ul class="autocomplete" data-role="listview" data-inset="true" data-filter="true"
data-filter-placeholder="street" data-filter-theme="e"></ul>
</div>
Code in the head:
<script>
$('#streetfield ul').listview();
var gcity = window.localStorage.getItem("city");
$(document).on( "pageinit", "#form", function() {
$(".autocomplete").on("listviewbeforefilter", function(e, data ) {
var $ul=$(this);
var value = $( data.input ).val();
var dropdownContent = "" ;
$ul.html("") ;
if ( value && value.length > 2 ) {
var response = ['data1','data2','data3'];
$('.autocomplete').show();
$ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading' ></span></div></li>" );
$ul.listview( "refresh" );
$.each(response, function( index, val ) {
dropdownContent += "<li>" + val + "</li>";
$ul.html( dropdownContent );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
});
}
});
});
$(document).on( "click", ".autocomplete li", function() {
var selectedItem = $(this).html();
$(this).parent().parent().find('input').val(selectedItem);
$('.autocomplete').hide();
});
</script>
Well, nobody helped. I think, this may be the solution.
I found it in an other case.
var x = $(#id).attr('data-lastval')
maybe it can help somebody.

Using JQueryUI Dialog, how can I remove a table row that is displayed using <A>

Can anyone assist with how to successfully use JQueryUI Dialog where I remove a table row that is displayed in the dialog by using a link or some other mechanism?
Here is a visual of the code I'm working with
http://i51.tinypic.com/30sh55t.png
HTML
<tr>
<td>1/1/0001</td>
<td>THOMAS</td>
<td>LNAMEONE<input type="hidden" id="name_17053942" value="LNAMEONE" /></td>
<td>SKIN TW</td>
<td>17053942<input type="hidden" id="number_17053942" value="SKINNER" /></td>
<td>1/7/2009</td><td>Graph Trend View Add</td>
<td class="hide-cell">
<!-- ui-dialog -->
<div id="dialog_17053942_message" title="17053942 THOMAS SKINNER">
<p></p>
</div>
</td>
</tr>
<tr>
<td>6/21/2010</td>
<td>EMERY</td>
<td>LNAMETWO<input type="hidden" id="name_ZF365763B" value="LNAMETWO" /></td>
<td>RAND E</td>
<td>ZF365763B<input type="hidden" id="number_ZF365763B" value="ZF365763B" /></td>
<td>1/7/2009</td><td>Graph Trend View Add</td>
<td class="hide-cell">
<!-- ui-dialog -->
<div id="dialog_ZF365763B_message" title="ZF365763B EMERY RANDOLPH">
<p></p>
</div>
</td>
</tr>
JQUERY
$(function() {
// Dialog
$('#carrier-list').dialog({
autoOpen: false,
resizable: false,
height: 260,
modal: true,
width: 240,
buttons: {
"Compare Carriers": function() {
$(this).dialog('close');
},
"Save": function() {
$(this).dialog('close');
}
}
});
// Dialog Link
// This adds a carrier to a list
$('.ui-state-add').click(function() {
var target = $(this).attr("id");
//alert(target);
$("#carrier-table").prepend("<tr id='" + target + "'>" +
"<td><a href='#' id='" + target + "' class='ui-state-remove ui-corner-all'>remove</a></td>" +
"<td>" + target + "</td>" +
"<td>" + $("#name_" + target).val() + "</td>" +
"</tr>");
$('#carrier-list').dialog('open');
return false;
});
//hover states on the static widgets
$('.ui-state-add').hover(
function() { $(this).addClass('ui-state-hover'); },
function() { $(this).removeClass('ui-state-hover'); }
);
// Remove Dialog Link
// This adds a carrier to a list
$('.ui-state-remove').click(function() {
var target = $(this).attr("id");
alert(target);
$('#' + target).remove(); ;
//$('#carrier-list').dialog('open');
return false;
});
});
I came up with a solution.
<script type="text/javascript" id="removeCarrier">
function removeCarrierFromList(obj) {
var i = obj.parentNode.parentNode.rowIndex;
document.getElementById('carrier-table').deleteRow(i);
$('#' + obj.id).removeClass('delete-carrier-company');
//alert(obj.id); //.hasClass('add-carrier-company').tostring() ); //
$('#' + obj.id).bind('click', function() {
//alert('User clicked on ' + obj.id);
});
}
</script>

Jquery autocomplete not updating the result in the UI

I have a HTML structure like:
<label for="supervisors">Assign the Supervisor: </label>
<input class="supervisors"/>
and my Javascript is:
<script type="text/javascript">
jQuery(function() {
jQuery( ".supervisors" ).autocomplete({
source: "/users/autocomplete_for_supervisors",
minLength: 2,
select: function( event, ui ) {
log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
and in my controller I have added the action for autocomplete_for_supervisors which is working when I gave in the URL as:
http://localhost:3000/users/autocomplete_for_supervisors?term=ar
But when I type something in the input box in my firebug the jquery is excuted and calling the respective function and returning the response in the firebug. But I am not getting the response below in the input box.
How do I get my returned result below in my input text box?
Please provide suggestions.
I think you are usingthe wrong variable names in the function / file that actually retrieves your data.
This is very similar to my problem which got solved: Having problems with jQuery UI Autocomplete

Resources