0

After investigating several methods to manipulate select menu options and cant get my head around the correct way , i try it here.

I have the following fiddle representing partial working code:

jsFiddle

Currently if re-select an option, it duplicates the text, but beside that unwanted effect , i actual want all options directly replaced.

The default code i use:

//START Get the stockdata $stock_array = array(); $products_stock_query=tep_db_query("SELECT * FROM " . TABLE_PRODUCTS_STOCK . " WHERE products_id=" . (int)$HTTP_GET_VARS['products_id'] ." ORDER BY products_stock_attributes"); while($products_stock_values=tep_db_fetch_array($products_stock_query)) { $str = $products_stock_values['products_stock_attributes']; $str = substr( $str, ( $pos = strpos( $str, '-' ) ) === false ? 0 : $pos + 1 ); echo '<div class="stockdata" valuestock="'.$products_stock_values['products_stock_quantity'].'" valueid="'.$str.'"></div>'; } ?> <script> $('option').each(function () { $(this).data('txt', $(this).text()); }); $('select').change(function () { var str = ""; $( "select option:selected" ).each(function() { str += this.text; $('option', this).each(function () { $(this).text($(this).data('txt')); }); }); var myCheck = $("body").find("div[valueid=" + $(this).find('option:selected').attr('value') + "]").attr("valuestock"); if (myCheck === '0'){ //do nothing }else{ // $(this).find('option:selected').text($(this).find('option:selected').attr('value')); $(this).find('option:selected').text(str + ' (' + $("body").find("div[valueid=" + $(this).find('option:selected').attr('value') + "]").attr("valuestock")+') <?php echo IN_STOCK_ATT; ?>'); } }).trigger( "change" ); </script> 
7
  • FYI, instead of using hidden DIVs to store relative data, use data-* attribute set directly on options, would be better Commented Apr 5, 2014 at 13:38
  • i am aware of the data attributes , but in this code i cant implement them.i already did some test with it.There is also no way to update jquery to the latest version as it will break a few other js codes.it is a dirty trick i have to use. Commented Apr 5, 2014 at 13:41
  • Well, data-attribute is quite well supported: caniuse.com/dataset Commented Apr 5, 2014 at 13:43
  • You have a $('option', this).each(function() {..}) inside of $("select option:selected").each(function () {}). What is this? o.o Commented Apr 5, 2014 at 13:44
  • BTW, if you want to set all option text on page load, why are you setting it inside onchange event of SELECT element? Why not set it server side while rendering HTML? Maybe i don't understand what you are looking for Commented Apr 5, 2014 at 13:45

1 Answer 1

1

Could be what you are looking for:

DEMO

$('select').find('option').each(function () { var numInStock = $("body").find("div[valueid=" + this.value + "]").attr("valuestock"); if (numInStock) { $(this).text($(this).text() + '(' + numInStock + ') In Stock'); } else { $(this).text($(this).text() + ' - Out of Stock'); } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

EXACTLY!!!!!!!!!!! so less code, i knew it would not be much need.But therefore experts like you exist.I am really thankfull for your help.
Glad you have fixed your issue ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.