2

I have the following code that is automatically generated, I want jQuery code help me to take out the value inside the div and add into a dropdown menu, and add "/listing-agent-staff/barack-obama" as option value.

the auto generated code:

<div class="StaffName">Barack Obama</div>
<div class="StaffName">Bill Clinton</div>
<div class="StaffName">Will Smith</div>

the dropdown menu I want the value to be inserted to:

<label for="Listing_Agent_Staff">Listing Contact</label>
<select name="Listing_Agent_Staff" id="Listing_Agent_Staff" class="cat_dropdown">
<option value="">-- Please select --</option>
<option value="/listing-agent-staff/barack-obama">Barack Obama</option>
<option value="/listing-agent-staff/bill-clinton">Bill Clinton</option>
<option value="/listing-agent-staff/will-smith">Will Smith</option>
</select>

please note the first part is generated automatically so the number of record varies, the number of options in the second part should cope with that. Thank you.

5 Answers 5

3

Use the following:

​$(".StaffName")​​​​​​​​.each(function () {
   var name = $(this).text();
   var value = "/listing-agent-staff/" + name.toLowerCase().replace(" ", "-");
   $("<option>").text(name).attr("value", value).appendTo("#Listing_Agent_Staff");
}​);​

Here's a DEMO.

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

Comments

2
var options = '';

$('.StaffName').each(function() {
    var val = $.text(this),
        url = val.toLowerCase().replace(/ /g, '-');
    options += '<option value="/listing-agent-staff/' + url + '">' + val + '</option>';
});

$('#Listing_Agent_Staff').append(options);

Here's the fiddle: http://jsfiddle.net/MCKZ7/

Comments

1

All you have to do is iterate over all the StaffName elements and append the apropriate HTML to the select.

$.each('.StaffName',function(index,elem){
  var oldValue = $(elem).text().toLowerCase();
  var optionValue = '/listing-agent-staff/' + oldValue.replace(' ','-');
  $("#Listing_Agent_Staff").append('<option value="'+ optionValue +'">'+ $(elem).text() +'</option>';
});

What we are doing here is iterating over all the .StaffName elements and for each one, extracting it's text value. That value is then converted to lower case and all the spaces are replaced with dashes. This is to create the URL. Then we use the append() function to add additional HTML content to the #Listing_Agent_Staff element.

Comments

1

Agreed with the previous answer that it would be ideal to do this on the server side. If that won't work, the following should take care of it:

$(function() {
  $('.StaffName').each(function(e) {
    var div = $(this);
    var href = '/listing-agent-staff/' + div.text().toLowerCase().replace(' ', '-');
    $('#Listing_Agent_Staff').append('<option value="' + href +'">' + div.text() + '</option>');
  });
});​

http://jsfiddle.net/hN67g/

Comments

0

This would mean having js write new code. The best solution is to have the server side code (php?) write out the dropdown code as well.

1 Comment

thanks...uhm....due to some reason, the server side coding for me is untouchable, this is why i have to do this in jquery.

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.