Today I searched Get JavaScript Object From jQuery trying to fix old code. I wanted to fix it by using some jQuery selectors but I still wanted to use most of the old JavaScript code. I initial passes up the jQuery page on the .get() method but then came back to it and was exactly what I needed.
Retrieve the DOM elements matched by the jQuery object.
So it was simple to get the first element of the selector with the get object.
var javascriptVar = $("#jQuerySelector").get(0);
This worked perfectly and allowed all of the old javascirpt to function with the more advanced selector than the good old document.getElementById()
Today I searched jQuery Sortable Placeholder Height Issue because when I dragged my sortable list the placeholder was shrinking to have a 0 height. It was tough tell where exactly I was moving it to. I needed to get this working or it would drive me crazy. So I tried a couple things before I found a solution. I made sure that there was a height on the element and in my jQuery call I used the options placeholder: ‘ui-state-highlight’ and forcePlaceholderSize:true, none of these things worked. Then I found that you could set the height of the placeholder on the start event of your sortable like this.
Today I searched for a JQuery UI Combobox. I found a custom widget on the jQuery UI site. By default it populates the combobox off of a select element. I was really wanting to load this off of a Ajax call instead, but only load the top 50 from the DB if they were to click on the arrow to browse. I made some modifications to the Combobox code to allow for the ‘source’ callback which is implemented on their current autocomplete. I also implemented the ‘select’ callback and ‘delay’ property.
I also have it setup to be able to combobox off of a text element if desired, instead of the select.
This widget should go into a js file and be included on the page
" )
.data( "item.autocomplete", item )
.append( "" item.label "" )
.appendTo( ul );
};
this.button = $( "" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.insertAfter( input )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-button-icon" )
.click(function() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
return;
}
// work around a bug (likely same cause as #5265)
$( this ).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
}).find('.ui-button-text').removeClass('ui-button-text'); //added the remove ui-button-text to remove the padding
//and make it the same size as our input
//see button in Message Center -> New Message for reference
},
destroy: function() {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
Here is an example usage of the widget:
$('#myTextField').combobox({
source: function(req,add) {
$.ajax({
url: "test.html",
dataType:"json",
success: function(data, textStatus, XMLHttpRequest){
add(data);
}
});
},
select: function(event, ui) {
//call back when the user selects an item on the list
alert('Item id selected [' ui.item.id ']');
},
delay: 500
});
Here is an example of json structure that the add() method takes above
Today I Searched how to make an AJAX call to a URL on a different domain. My first attempt failed with a security error. I dug into the problem a bit and found out that PHP can be used as a proxy to fetch the web content.
AJAX Calls PHP Script On Your Domain -> PHP Script Fetches Content From Remote Domain -> PHP Script Returns The Remote Data Back To The AJAX Call
Below is a sample PHP script that I found and tweaked.
// Set your return content type
header('Content-type: text/html');
// Website url to open
$daurl = 'http://www.otherdomain.com/index.php?sku=' . $_GET["sku"];
// Get that website's content
$handle = fopen($daurl, "r");
// If there is something, read and return
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
Below is the AJAX call to load the content into a div using JQuery
The script could be further modified to pass in the entire URL as a parameter so it could be more generic. I would love to hear about other cross domain AJAX options if anyone has any other suggestions.