Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
jquery - how to populate a select list from table values
Old 06-14-2012, 09:33 AM jquery - how to populate a select list from table values
TWD
TWD's Avatar
King Spam Talker

Posts: 1,296
Trades: 0
A simple question for jquery ninjas I'm sure.
(yes, i DO want to use jquery)

Lets say I have a 3 column table like this:

Code:
<table>
<tbody>
<tr class="sal-calc">
<td>Bob</td>
<td>Smith</td>
<td>50,000</td>
</tr>
<tr class="sal-calc">
<td>Bill</td>
<td>Green</td>
<td>40,000</td>
</tr>
<tr class="sal-calc">
<td>John</td>
<td>Brown</td>
<td>60,000</td>
</tr>
</tbody>
</table>
And I have a placeholder selection element like this:

Code:
<label>Select employee:</label>
<select class="emp-tbl">
  <option value="0"  selected="selected"> - </option>    
</select>
And I want to take the first two <td> values as the concatenated text
AND
take the third <td> value and use it as the "value" attribute for each selector.
So the end result should look like this:

Code:
<label>Select employee:</label>
<select class="emp-tbl">
  <option value="0"  selected="selected"> - </option>    
  <option value="50,000">Bob Smith</option>
  <option value="40,000">Bill Green</option>
  <option value="60,000">John Brown</option>
</select>
Hints, peoples?
__________________

Please login or register to view this content. Registration is FREE
"Order a PEBBLE Smart Watch for Bluetooth connection to your iPhone or Android" - 100% Waterproof - 7+ days Battery Charge - High Res Outdoor Readable - Vibrating Alert - Incoming Caller ID

Last edited by TWD; 06-14-2012 at 09:35 AM..
TWD is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-14-2012, 01:29 PM Re: jquery - how to populate a select list from table values
lizciz's Avatar
Super Spam Talker

Posts: 845
Name: Mattias Nordahl
Location: Sweden
Trades: 0
This may work, although it is totally untested.

Code:
$(function(){
    $("table tr.sal-calc").each(function(){
        var val, txt;
        var td = $(this).children().first();
        txt = td.text();

        td = td.next();
        txt += " " + td.text();

        td = td.next();
        val = td.text();

        var opt = $("option").val(val).text(txt);

        $("select.emp-tbl").append(opt);
    });
});
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 06-14-2012 at 01:32 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-14-2012, 07:45 PM Re: jquery - how to populate a select list from table values
TWD
TWD's Avatar
King Spam Talker

Posts: 1,296
Trades: 0
Quote:
Originally Posted by lizciz View Post
This may work, although it is totally untested.

Code:
$(function(){
    $("table tr.sal-calc").each(function(){
        var val, txt;
        var td = $(this).children().first();
        txt = td.text();

        td = td.next();
        txt += " " + td.text();

        td = td.next();
        val = td.text();

        var opt = $("option").val(val).text(txt);

        $("select.emp-tbl").append(opt);
    });
});
Thank you. I finally got what I wanted with something very similar to your solution. cheers.
__________________

Please login or register to view this content. Registration is FREE
"Order a PEBBLE Smart Watch for Bluetooth connection to your iPhone or Android" - 100% Waterproof - 7+ days Battery Charge - High Res Outdoor Readable - Vibrating Alert - Incoming Caller ID
TWD is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to jquery - how to populate a select list from table values
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13849 seconds with 11 queries