Reputation: 2656
I downloaded a livesearch javascript plugin for my site, it's working, but I need help to modify it a little bit.
Here is the code:
$.ajax({url: $('base').attr('href') + 'index.php?route=product/search/ajax&keyword=' + keywords, dataType: 'json', type: 'GET', success: function(result) {
if( result.length > 0 ) {
var eList = document.createElement( 'ul' );
eList.id = 'livesearch_search_results';
var eListElem;
var eLink;
var eHref;
for( var i in result ) {
eListElem = document.createElement( 'li' );
eLink = document.createElement( 'a' );
if( result[i].img != null ) {
eImg = new Image();
eImg.src = result[i].img;
}
else {
eImg = document.createElement( 'span' );
eImg.innerHTML = ' ';
}
eDiv = document.createElement( 'div' );
eLink.appendChild( document.createTextNode(result[i].name) );
if( typeof(result[i].href) != 'undefined' ) {
eHref = result[i].href;
}
else {
eHref = $('base').attr('href') + 'index.php?route=product/product&product_id=' + result[i].product_id + '&keyword=' + keywords;
}
eLink.href = eHref;
eDiv.appendChild( eLink );
eDiv.innerHTML = eDiv.innerHTML + result[i].desc;
eListElem.appendChild( eImg );
eListElem.appendChild( eDiv );
eListElem.appendChild( document.createElement('br') );
eListElem.setAttribute( 'rel', eHref );
$(eListElem).bind('click', function(){
var gto = $(this).attr( 'rel' );
try {
if( gto != false && gto.length > 0 ) {
document.location = gto;
}
}
catch( e ) {}
});
eList.appendChild( eListElem );
}
if( $('#livesearch_search_results').length > 0 ) {
$('#livesearch_search_results').remove();
}
$('#search_menu').append(eList);
}
}});
The output is something like this:
<li rel="http://url"><img src="http://url.jpg">
<div><a href="url">TITLE</a>Description</div>
<br></li>
Is there any way to add a element to the tag? Like this:
<li rel="http://url"> <span class="img_container"><img src="http://url.jpg"> </span>
<div><a href="url">TITLE</a>Description</div>
<br></li>
I know, that I can create a tag with document.createElement( 'span' )
, but I don't know where is exactly going in this case.
Upvotes: 1
Views: 323
Reputation: 664297
Just replace the lines
eImg = new Image();
eImg.src = result[i].img;
with
var eImg = document.createElement( 'span' );
eImg.className = "img_container";
var img = new Image();
img.src = result[i].img;
eImg.appendChild( img );
Btw, the script author forget to declare the variable eImg
as local. I doubt this was on purpose.
Upvotes: 2