Solved

JQuery dynamic change of descriptions in dropdownlist

Posted on 2013-10-30
10
221 Views
Last Modified: 2013-11-19
I have a dynamically vreated dropdownlist with some monetary ranges show like this:

0 to 500
501 to 1000
1001 to 2000
...

Open in new window


I have the following code:

<option value="<%=(RS_Fee01("FeeId"))%>"><span id="currencyindicator"></span><%=(RS_Fee01("AmountFrom"))%> to <span id="currencyindicator"></span><%=(RS_Fee01("AmountTo"))%></option>

Open in new window


I would like it to show the values like this:

£0 to £500
£501 to £1000
£1001 to £2000
...

Open in new window


Or this:

$0 to $500
$501 to $1000
$1001 to $2000
...

Open in new window


I would like to use the JQuery to set a value in the #currencyindicator span on change of currency (another dropdownlist on the same form).

As a quick and dirty test set I set a hard coded value to see if I could get the dropdownlist description updated. It didn't work!

$('#currencyid').change(function() {
	$('span#currencyindicator').text("£");
});

Open in new window


Doesn't seem to be cutting it... ideas?

Regards,
0
Comment
Question by:splanton
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39611816
test page : http://jsfiddle.net/fvq5D/

$(document).ready(function(){
    $('#currencyid').change(function() {
        createDropdown($(this).val());
    }).change();
});
function createDropdown(currency) {
    var index = $('#mySelect').prop('selectedIndex');
    $('#mySelect').empty();
    for(var i=0;i<2000;i+=500) {
        var min = currency + (i+(i>0?1:0));
        var max = currency + (i+500);
        var str = min + ' to ' + max;
        $('#mySelect').append('<option value="' + str + '">' + str + '</option>');
    }
    if(index>=0) $('#mySelect').prop('selectedIndex', index);
}

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39611853
leakim971 is much better at this stuff then I am and I am sure this can be improved.  

I noticed you are using asp to dynamically generate your code.  One option would be to use asp to write out your js.  If you need to write out html.  Something like below where you are writing your range in the html option data-from and data-to.  You can then access that via jquery to generate the option text.

http://jsbin.com/EyoyEFU/1/edit

<select id="currency">
       <option value="$">$</option>
     <option value="£">£</option>
  </select>  
  
  <select id="money">
  <option value="1" data-from="0" data-to="500"></option>
  <option value="2" data-from="501" data-to="1000"></option>
  <option value="3" data-from="1001" data-to="2000"></option>
  </select>

Open in new window


$(function() {
var currencyindicator='$';
$('select#currency').change(function(){
  currencyindicator=$(this).val();
  $('#money option').each(function(){
     var from=$(this).attr('data-from');
      var to=$(this).attr('data-to');
    $(this).text(currencyindicator+from+'-'+currencyindicator+to);
  });
});
  $('#money option').each(function(){
     var from=$(this).attr('data-from');
      var to=$(this).attr('data-to');
    $(this).text(currencyindicator+from+'-'+currencyindicator+to);
  });
  
  
}); 
  

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39611862
  <select id="money">
  <option value="1" data-from="0" data-to="500"></option>
  <option value="2" data-from="501" data-to="1000"></option>
  <option value="3" data=from="1001" data-to="2000"></option>
  </select>

Open in new window

convert to


  <option value="<%=(RS_Fee01("FeeId"))%>" data=from="%=(RS_Fee01("AmountFrom"))%>" data-to="%=(RS_Fee01("AmountTo"))%>"></option>

Open in new window

0
How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39611878
Also, remember in your html, id's are unique, classes are not.  

In your original code you had, <span id="currencyindicator">.  Besides the fact the span does not work in the option, it would have created multiple id's.  Had that worked, you would have wanted to use a class instead of id.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39611981
Always good to have options:

$('#currencyid').change(function() {
	var currency = $(this).val();
	
	$('#money option').each(function() {
		var values = $(this).text().split(' to ');
		$.each(values, function(i, val) {
			values[i] = currency + values[i].replace( /^\D+/g, '');	
		});
		$(this).text(values.join(' to '));
	});
});

Open in new window

<select id="currencyid">
	<option value="$">Dollar</option>
	<option value="£">Pound</option>
</select>


<select id="money">
	<option value="1">0 to 500</option>
	<option value="2">501 to 1000</option>
	<option value="3">1001 to 2000</option>
</select>

Open in new window

An option can't contain HTML so you can't include <spans> etc..
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39611998
Oops - Forgot the fiddle - http://jsfiddle.net/ChrisStanyon/AbSML/
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39612187
>Always good to have options:
and a very good one at that!
0
 
LVL 58

Expert Comment

by:Gary
ID: 39612217
Why not just use CSS :before or am I missing something?

e.g.

li span:before{
content: "$";
}
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39612262
@Gary - the OP needs to dynamically change <options> based on another dropdown.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39612378
Ahh thought it was a UL not a select - didn't read that properly.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question