CodingHTML radio buttons

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  milko  
 To:  99% of gargoyles look like (MR_BASTARD)     
38188.73 In reply to 38188.71 
well, it's quite easy to add most of the workarounds too. And why would anyone bother? I don't really know why anyone would type it in the first place mind you, so I maybe at an understanding-disadvantage here.

milko
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  af (CAER)     
38188.74 In reply to 38188.72 
Oddly enough, I find i++ easier to read, although I agree it's less flexible.

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  milko     
38188.75 In reply to 38188.73 
quote: milko
And why would anyone bother?

To maintain freedom of speech, deny MOD MADNESS, and retain the original meaning. Except I'm not sure what it is.

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Radio   
 To:  af (CAER)     
38188.76 In reply to 38188.68 
So would that pull the values from the rows of the table, or would I have to rewrite the table to support the function?
My life is hard, I suffer lots
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Radio      
38188.77 In reply to 38188.76 
Well as it is you'd have to rewrite the table, but this would take the values from the table itself:
javascript code:
var checkBoxes = document.querySelectorAll("input[type=checkbox]"),
    max = checkBoxes.length,
    i, total = 0, totalWholesale = 0;
 
for (i = 0; i < max; i += 1) {
    if (checkBoxes[i].checked) {
        // Get values directly from table cells.
        total += +checkBoxes[i].parentElement.
            previousElementSibling.innerHTML;
        totalWholesale += +checkBoxes[i].parentElement.
            previousElementSibling.previousElementSibling.innerHTML;
    }
}


edit: just realised the + should be at the start of the variable names, not the end. The + at the start is a forced type conversion; an alternative is to use:
javascript code:
total += parseInt(checkBoxes[i].parentElement.previousElementSibling.innerHTML, 10);


Edit again:
Another thing I thought of was that using jQuery needn't be any hassle - you can just include this line on your page somewhere before your current JS code:
HTML code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>


edit3: oo, jQuery 1.5 is out :O
0/0
 Reply   Quote More 

 From:  Matt  
 To:  af (CAER)     
38188.78 In reply to 38188.77 
quote:
edit3: oo, jQuery 1.5 is out


Hooray. I wonder if it breaks as much stuff as 1.3 to 1.4 did.

doohicky

0/0
 Reply   Quote More 

 From:  Radio   
 To:  af (CAER)     
38188.79 In reply to 38188.77 

So what you're saying is that I should remove all the 20 individual inspections of the checkboxes, and replace with just this code? And that it will produce the same page?

 

That doesn't work, and I'm not sure what else I'd need to do.

My life is hard, I suffer lots
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  af (CAER)     
38188.80 In reply to 38188.77 

It's probably just me being anally retentive, but I bloody hate innerHTML, it's not part of the DOM and it's queer.

 

And by "queer" I mean gay and homosexual, not as in "by jove, that's a rum do and no mistake".

 

And by "gay" I mean queer and homosexual, not "having a gay old time, dahling".

 

It's a wonderful thing, this English language malarkey, isn't it?

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Radio      
38188.81 In reply to 38188.79 
If he'd used jQuery it would have worked! ;)

What does the error in the console say?
0/0
 Reply   Quote More 

 From:  Radio   
 To:  Peter (BOUGHTONP)     
38188.82 In reply to 38188.81 

Object doesn't support this property or method (line 104, char 1)

 

Line 104 is:
var checkBoxes = document.querySelectorAll("input[type=checkbox]"),

My life is hard, I suffer lots
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Radio      
38188.83 In reply to 38188.82 
Are you using an old version of IE?
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  99% of gargoyles look like (MR_BASTARD)     
38188.84 In reply to 38188.80 
What would you suggest instead?
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Radio      
38188.85 In reply to 38188.82 
Add this code to the top of the <script> section:
javascript code:
var getCheckboxes = function () {
    var i, elements = document.getElementsByTagName("input"),
        max = elements.length,
        result = [];
 
    for (i = 0; i < max; i += 1) {
        if (elements[i].type === "checkbox") {
            result.push(elements[i]);
        }
    }
    return result;
};
Then change the line with the error so it reads:
javascript code:
var checkBoxes = getCheckboxes,
If that still doesn't work, post the code you have and I'll see if I can spot the problem.

Btw, the reason it's best to put all your var declarations at the start of the function is that it helps you get clear in your head what the function needs and does. When you start declaring them inline whenever and wherever, it becomes difficult to remember what does what.
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  af (CAER)     
38188.86 In reply to 38188.84 
I've kind of lost the plot as to why a table value is being extracted, but if it's associated with a check box I'd be tempted to read the name, value, and checked attributes of the input.

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Radio   
 To:  af (CAER)     
38188.87 In reply to 38188.85 

Here you go - it now complains that f5 isn't defined (which sort of makes sense as I used to use that to calculate my totals).
I could remove the totaliser line (line 135) but no idea what to put in it's place.

 

As for why I'm reading a value from a table, have a look at the file in IE and it should make more sense. This is about choosing a base product from a choice of 4, and then adding various options.
Radio boxes are used for the initial selection, and then checkboxes for the rest. The total then displays the price for your selection.

My life is hard, I suffer lots

Attachments:
PL3.htm

0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Radio      
38188.88 In reply to 38188.87 
Ok that's easy enough - the 'Calculate grand total' lines just need to be changed to this:
javascript code:
var GrandTotal = f1 + total,
    GrandTotalW = w1 + totalWholesale;
Oh and if you want to make Truffy happy and/or stick to the standards, you can change the .innerHTML bits in the loop to .firstChild.nodeValue

edit:
Sorry, I just noticed a mistake I made earlier, too - this line:
code:
var checkBoxes = getCheckboxes,
should read:
code:
var checkBoxes = getCheckboxes(),
Note the brackets - they assign the return value of the function to the variable checkBoxes, rather than assigning it the function itself.
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  99% of gargoyles look like (MR_BASTARD)     
38188.89 In reply to 38188.86 
That'd be fine if the checkboxes were created (by hand, I'm guessing) with a value attribute in the first place :)
0/0
 Reply   Quote More 

 From:  Radio   
 To:  af (CAER)     
38188.90 In reply to 38188.88 

Still not working. I noticed one error myself, you'd referred to a variable as GrandTotalW rather than GrandWTotal, but then I got this next one:

 

Message: 'parentElement.previousElementSibling.innerHTML' is null or not an object
Line: 126
Char: 9
Code: 0

 

HTML is attached.

 

(by the way, this is why I prefer my method, it may be more long winded and cumbersome, but it's a damn site easier to understand and fix when it goes wrong)

My life is hard, I suffer lots

Attachments:
PL3.htm

0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Radio      
38188.91 In reply to 38188.90 
Once again, it's an issue with IE, in this case not supporting the previousElementSibling property. Change it to previousSibling and it works fine. I've only tried it in IE8, dunno about 7 or 6.
0/0
 Reply   Quote More 

 From:  Radio   
 To:  ALL
38188.92 
I'm only worried about 8, and all seems to work fine now - thanks.
My life is hard, I suffer lots
0/0
 Reply   Quote More 

Reply to All  
 

1–20  21–40  41–60  61–80  81–95

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats