0

I have a page with a search bar that has auto complete according to the data in a separate js file. If I type in a letter it throws out the related names that starts with or contains that letter, if I select the result if displays the details that come with that name. I just need to know how to link an an image to that data so when selected a picture is displayed with that info of the name.

Here is my Javascript code:

$(function(){

    var currencies = [
        { value: 'Murray Smith', data: 'AFN', foto: src='img/logo.jpg' },
        { value: 'Brown Church', data: 'ALL' ,foto: src='../img/logo.jpg'},
        { value: 'Jack Jones', data: 'DZD' ,foto: src='../img/logo.jpg'},
        { value: 'Ben Clark', data: 'EUR' ,foto: src='../img/logo.jpg'},
        { value: 'Pete White', data: 'AOA' ,foto: src='../img/logo.jpg'},
        { value: 'East Caribbean dollar', data: 'XCD' ,foto: src='../img/logo.jpg'},
    ];

    // setup autocomplete function pulling from currencies[] array
    $('#autocomplete').autocomplete({
        lookup: currencies,
        onSelect: function (suggestion) {
            var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value + ' <br>      <strong>Symbol:   </strong> ' + suggestion.data + '<br> <strong>Profile Pic:</strong> ' +     suggestion.foto;
            $('#outputcontent').html(thehtml);
        }
    });
}); 

Thank you in advance.

2
  • Why don't use src: '../image/logo.jpg'? I am sure that would work :) Commented Oct 17, 2013 at 13:27
  • 5
    You can start by writing valid JavaScript. foto: src='img/logo.jpg' contains syntax errors. Commented Oct 17, 2013 at 13:27

3 Answers 3

2

Use this code:

var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value 
             + ' <br>  <strong>Symbol:   </strong> ' + suggestion.data 
             + '<br> <strong>Profile Pic:</strong> <img ' + suggestion.foto +' />';
                                                  ^^^ use image tag here

And your foto should be like this:

foto: "src='img/logo.jpg'"
     ^^^ add the double quotation mark to make it correct
Sign up to request clarification or add additional context in comments.

3 Comments

I like yours more :), but... mine will still work :p
@JamieHutber yah you are also right. and your code will also work. but in your code there is redundancy of code as img tag is used multiple times. but approach is correct.
exactly, lets just assume he wants separate things in each img, for example an alt tag.... (he could just add another variable, yeah yeah)
0

Updated your array to be valid will help :)

 var currencies = [
      { value: 'Murray Smith', data: 'AFN', foto: '<img src="img/logo.jpg">' },
      { value: 'Brown Church', data: 'ALL' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Jack Jones', data: 'DZD' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Ben Clark', data: 'EUR' ,foto: '<img src="img/logo.jpg">'},
      { value: 'Pete White', data: 'AOA' ,foto: '<img src="img/logo.jpg">'},
      { value: 'East Caribbean dollar', data: 'XCD' ,foto: '<img src="img/logo.jpg">'},
 ];

1 Comment

Referencing the url instead of using html inside of the currencies array would improve reusability and reduce repetition. value: 'Murray Smith', data: 'AFN', foto: 'img/logo.jpg' should be sufficient.
0

As André Dion mentioned in his comment, your syntax is invalid.

you should change the foto property value of your object to be:

foto: 'path/to/my/img.ext'

and in your HTML, when calling that property, append it inside an image tag.

thehtml += '<img src="'+foto+'" alt="don't forget the blind!" />';

Hope this helps.

‐ Sid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.