JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 12th, 2013, 06:50 PM
kattie kattie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 kattie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 19 sec
Reputation Power: 0
Other - Retrieve data from dynamic drop down

Hi I have a problem with retrieving first populated, than selected by user quantity from the select dropdown menu.
I have several the same numeric type of menus. They contain values 0-20.*
Event onchange causes the selected number to be displayed in an html div. However, if I try to get the number back from div for my calculations, it doesn't work.*
I spent hours on it, any help greatly appreciated....

function showNumber(selectId, divNumber) {

* var select = document.getElementById(selectId);
* var noItems = document.getElementById(divNumber);

* for (var i = 0; i <= 20; i++) {

* * var option = document.createElement('option');
* * option.innerHTML = i;
* * option.value = i;

* * select.appendChild(option);
* }

* select.onchange = function () {
* * var noZero = select.value;
* * if (noZero > 0) {
* * **noItems.innerHTML = noZero;
* * } else*noItems.innerHTML = " ";
* };
}



* function showDivs(){
showNumber('item1', 'div1');
showNumber('item2', 'div2');
showNumber('item3', 'div3');
showNumber('item4', 'div4');
}

etc.

Reply With Quote
  #2  
Old January 12th, 2013, 10:26 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 30 m 44 sec
Reputation Power: 69
Looks like you had a few operator and string conversion errors, in your code; try this:

Code:
function showNumber(selectId, divNumber) {

var select = document.getElementById(selectId);
var noItems = document.getElementById(divNumber);
var option = document.createElement('option');

for (var i = 0; i <= 20; i++) {
option.innerHTML += i;
option.value += i;
select.appendChild(option);
}

select.onchange = function () {
var noZero = select.innerHTML;
noZero = parseInt(noZero);
if (noZero > 0) {
noItems.innerHTML = noZero;
}
else {
noItems.innerHTML="";
}

}

Reply With Quote
  #3  
Old January 12th, 2013, 11:34 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 40 sec
Reputation Power: 4192
Welcome to DevShed Forums, kattie.

The code you posted looks fine. Perhaps the reason why your calculations (which you aren't showing us) don't work is that you need to convert the string to a number. One way to do that conversion is with parseInt(), as web_loone08 showed, however, it's best to specify a second argument of "10" to make sure that any numbers with leading zeros aren't converted to octal.

Using asterisks to preserve indentation is unusual. In the future it would be helpful if you put your code between [code][/code] tags instead.

Quote:
Originally Posted by web_loone08
Looks like you had a few operator and string conversion errors, in your code

What operator errors? The attribute values aren't set to anything, so appending strings to them is unnecessary.

If I remember correctly, creating the option element once outside the loop will only generate it once, it will just change the value and re-append it for each loop. You could, however, use the cloneNode() method to do something similar, which might result in a very small performance boost.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #4  
Old January 12th, 2013, 11:54 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 30 m 44 sec
Reputation Power: 69
Quote:
Originally Posted by Kravvitz
If I remember correctly, creating the option element once outside the loop will only generate it once, it will just change the value and re-append it for each loop.


Uh... yeah, that's my fault; did not test like usual... I got lazy and glanced this one over. But in my defense; I totally am going to blame it on the benadryl; that I took a couple hours ago.

Quote:
Originally Posted by Kravvitz
What operator errors? The attribute values aren't set to anything, so appending strings to them is unnecessary.


I was thinking about the createElement being outside the loop and making sure the loop would not overwrite the option elements. So that's why I changed the operator from = to +=.

Last edited by web_loone08 : January 13th, 2013 at 12:00 AM.

Reply With Quote
  #5  
Old January 13th, 2013, 12:10 AM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 40 sec
Reputation Power: 4192
Quote:
Originally Posted by web_loone08
Uh... yeah, that's my fault; did not test like usual... I got lazy and glanced this one over. But in my defense; I totally am going to blame it on the benadryl; that I took a couple hours ago.

Does that medication have any warnings about heavy machinery? If so, maybe they should add a warning about programming too.
Comments on this post
web_loone08 agrees: Haha, yeah... probably so.

Reply With Quote
  #6  
Old January 13th, 2013, 02:53 AM
kattie kattie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 kattie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 19 sec
Reputation Power: 0
Hi
Thank you for welcoming me. Good day to you!

What I'm trying to do is to simply get (from the drop down) selected number of products, and then multiply it by the price (done in the same select loop. I only added more parameters to both functions).
But, once I want to take all the results and add all the prices together, I can't.

And yes, I do parsing. So now it looks like:

Code:
function showNumber(selectId, divNumber, prodName, calcPrice) {

  var select = document.getElementById(selectId);
  var noItems = document.getElementById(divNumber);
  var product = prodName.itemName;
  var getPrice = floatPrice(calcPrice);
  
  for (var i = 0; i <= 20; i++) {

    var option = document.createElement('option');
    option.innerHTML = i;
    option.value = i;

    select.appendChild(option);
  }

  select.onchange = function () {
    var noZero = select.value;
/*parseValue takes the same select.value and parses it, as otherwise the variable is concatenated with the string in the answer and can't perform in calculations*/
    var parseValue = parseInt(select.value,10);
    if (noZero > 0) {
      var itemsPrice = parseFloat(parseValue * getPrice);
      divQuantity.innerHTML = noZero +" "+ product + " "+ "£"+ itemsPrice;
      
      
    } else divQuantity.innerHTML = " ";
  };

}
  function showDivs(){
displayQuant('monkey', 'divQuant1', Monkey, Monkey);
displayQuant('bear', 'divQuant2', Bear, Bear);


Quote:
Originally Posted by Kravvitz
Welcome to DevShed Forums, kattie.

The code you posted looks fine. Perhaps the reason why your calculations (which you aren't showing us) don't work is that you need to convert the string to a number. One way to do that conversion is with parseInt(), as web_loone08 showed, however, it's best to specify a second argument of "10" to make sure that any numbers with leading zeros aren't converted to octal.

Using asterisks to preserve indentation is unusual. In the future it would be helpful if you put your code between [code][/code] tags instead.


What operator errors? The attribute values aren't set to anything, so appending strings to them is unnecessary.

If I remember correctly, creating the option element once outside the loop will only generate it once, it will just change the value and re-append it for each loop. You could, however, use the cloneNode() method to do something similar, which might result in a very small performance boost.

Reply With Quote
  #7  
Old January 13th, 2013, 09:09 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 16 h 30 m 44 sec
Reputation Power: 69
Why don't you insert the prices into an array and then you can access them, at a later time, to do multiplication or addition or any other type of math; that you want to use on them?

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - Retrieve data from dynamic drop down

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap