0

I have this code that somebody helped me with here, it basically converts select options to div elements and displays them with an image alongside them.

 $('#property_types option').slice(1).each(
function(){
    var text = $(this).text(),
        outputTo = $('#output'),
        div = $('<div />').text(text),
        img = $('<img />', {'src' : text}).prependTo(div);
    div.appendTo(outputTo);
});

right now, the output is as follows:

 <div><img src="Apartment" /> Apartment</div>
 <div><img src="Building" /> Building</div>
 <div><img src="Land" /> Land</div>
 <div><img src="Office" /> Office</div>

I want to dynamically adjust the src as well.

to make it like:

 <div><img src="images/Apartment.png" /> Apartment</div>
 <div><img src="images/Building.png" /> Building</div>
 <div><img src="images/Land.png" /> Land</div>
 <div><img src="images/Office.png" /> Office</div>
1
  • use img.attr('href', 'source'); Commented Aug 16, 2012 at 12:32

3 Answers 3

3

add text to src

$('#property_types option').slice(1).each(
function(){
    var text = $(this).text(),
        outputTo = $('#output'),
        div = $('<div />').text(text),
        img = $('<img />', {'src' : 'images/'+text+'.png'}).prependTo(div);
    div.appendTo(outputTo);
});

and

$('#property_types option').slice(1).each(
function(){
    var text = $(this).text(),
        outputTo = $('#output'),
        div = $('<div />').text(text),
        href = $('<a />', {'href' : '#', 'onclick' : 'go'+text+'();'}).prependTo(div),
        img = $('<img />', {'src' : 'images/'+text+'.png'}).prependTo(href);
    div.appendTo(outputTo);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm! Thanks a lot! What if I want to add a unique link to each entry? will this break it? <div><a href="#" onclick="goApartment()"><img src="images/Apartment.png" /> Apartment</a></div>
you should add it before img, and img prepend to href
Awesome! This took me hours and I couldn't figure out even the image part! You fixed both the problems, can't thank you enough!
2

Try this:

 $('#property_types option').slice(1).each(function(){
      var text = $(this).text(),
          outputTo = $('#output'),
          div = $('<div />').text(text),
          img = $('<img />', {'src' : 'images/'+text+'.png'}).prependTo(div);
      div.appendTo(outputTo);
 });

2 Comments

Why are you mixing double and single quotes in the src attribute?
No, they're both allowed. It just makes it harder to read the code if you're inconsistent.
1

try this

$(document).ready(function() {

    var source = 'http://upload.wikimedia.org/wikipedia/en/thumb/e/e9/Ruby_on_Rails.svg/150px-Ruby_on_Rails.svg.png'
    outputTo = $('#output');
    var div = $('<div />').text('sdfsdfdsf');
    var img = $('<img />').attr('src', source).prependTo(div);
    div.appendTo(outputTo);
});​

​

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.