6

HTML

<select class="selectAddress" name="select2" size="4" multiple="multiple">
    <option>address 1</option>
    <option>address 2</option>
    <option>address3, some city, uk</option>
    <option>address4, some city, uk</option>
    <option>address4, some city, uk</option>
</select>

<p id="chosenAddress01" class="renderedYellowBox">result in here</p>

jQuery

$(".selectAddress").dblclick(function() {
var address = [];
    $('.selectAddress option:selected').each(function(i, selected){ 
       address[i] = $(selected).text(); 
    });
    //alert(address);

    $('#chosenAddress01').html(address);
    });

Problem

I'm trying to get the selected value of the address option to populate the p tag on dblclick() of the address

If I use the alert box to check the result, the correct result comes thru. But trying to get the result into the p tag returns nothing.

Can anyone help?

Thanks, Kevin

3 Answers 3

6

Use .join() to turn it into a string first, like this:

$('#chosenAddress01').html(address.join(', '));

.html() treats an array differently, so best to explicitly make it a string since that's what you're after. The reason alert() works is there's an implicit .toString() going on there.

Sign up to request clarification or add additional context in comments.

Comments

3
  1. You're a brave man to use "dblclick"
  2. You'll want to join the array:

    $('#chosenAddress01').html(address.join(' '));

2 Comments

Thanks. Just finding my feet with jQuery at the moment, and discovering the power, and nuances of it. What's the issues related with dblClick()?
Well browsers just don't do a very good job with "dblclick" and "click". It might work out for you, but it makes me a little nervous because <option> elements have some native behavior on the "click" event already.
1

Address is an array, did you try to convert into a string?

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.