2

i have several js functions doing the same thing width different values for different divs. Here is the HTML-code:

<input id="kunst_radio1" type="radio" name="kunstmuseum" value="0" checked="checked"  onClick="animateKunst('null')"><label for="kunst_radio1"></label>
<input id="kunst_radio2" type="radio" name="kunstmuseum" value="6794200" onClick="animateKunst('halb')"><label for="kunst_radio2"></label>
<input id="kunst_radio3" type="radio" name="kunstmuseum" value="13588400" onClick="animateKunst('voll')"><label for="kunst_radio3"></label>
<input id="hist_radio1" type="radio" name="hist" checked="checked" onClick="animateHist('null')"><label for="hist_radio1"></label>
<input id="hist_radio2" type="radio" name="hist" onClick="animateHist('halb')"><label for="hist_radio2"></label>
<input id="hist_radio3" type="radio" name="hist" onClick="animateHist('voll')"><label for="hist_radio3"></label>

I have about 15 radio button groups and for each group I have to execute this function with different values:

function animateKunst(val) {
    var div = $('#kunstmuseum');
    if(div.data('originalWidth') == undefined)
    div.data('originalWidth', div.width());

    var width = div.data('originalWidth');

    if (val=="voll")
        width *= .6;
    else if (val=="halb") 
        width *= .8;

    $('#kunstmuseum').animate({width: width}, 500);
    }
function animateHist(val) {
    var div = $('#historisch');
    if(div.data('originalWidth') == undefined)
    div.data('originalWidth', div.width());

    var width = div.data('originalWidth');

    if (val=="voll")
        width *= .7;
    else if (val=="halb") 
        width *= .9;

    $('#historisch').animate({width: width}, 500);
    }

Can I put all the values in an array and then make a for-loop? How can I do that?

1 Answer 1

4

I would slightly modify the ids so that I can reuse them more easily inside my function and define a mapping for your widths.

<script type="text/javascript">

        var mapping = {
            kunst: {
                voll: 0.6,
                halb: 0.8
            },
            hist: {
                voll: 0.7,
                halb: 0.9
            },
            null: {
                voll: 0,
                halb: 0
            }
       };

       function animate(type, val) {

           var div = $('#' + type);
           if ( !div.data('originalWidth') ) {
               div.data('originalWidth', div.width());
           }

           var width = div.data('originalWidth');
           width *= mapping[type][val];
           div.animate({ width: width }, 500);

       }

    </script>

<input id="kunst_radio1" type="radio" name="kunst" value="0" checked="checked" 
    onClick="animate('kunst', 'null')">
<label for="kunst_radio1"></label> 
<input id="kunst_radio2" type="radio" name="kunst" value="6794200"
    onClick="animate('kunst', 'halb')">
<label for="kunst_radio2"></label> 
<input id="kunst_radio3" type="radio" name="kunst" value="13588400" 
    onClick="animate('kunst', 'voll')">
<label for="kunst_radio3"></label> 
<input id="hist_radio1" type="radio" name="hist" checked="checked" 
    onClick="animate('hist', 'null')">
<label for="hist_radio1"></label> 
<input id="hist_radio2" type="radio" name="hist" 
    onClick="animate('hist', 'halb')">
<label for="hist_radio2"></label> 
<input id="hist_radio3" type="radio" name="hist" 
    onClick="animate('hist', 'voll')">
<label for="hist_radio3"></label>
Sign up to request clarification or add additional context in comments.

6 Comments

Beat me to it, but this makes more sense to do it this way than to keep creating more and more animate functions. I would also move the mapping out of the function and perhaps make it a global, since you can then use the mapping to create other functions if necessary.
@QuantumLicht it doesnt work in Chrome but in safari and firefox, what could be the problem?
First of all, I would probably change the "null" keyword to something else. It is a reserved keyword and this is likely why it does not work in chrome. Can you give me more details about what you mean by "doesn't work" ? Can you try and pinpoint exactly what part does not work?
I changed this value, thanks for suggestion. The animate function doesnt work. If I click on a radio button, in chrome the div size is not changing. See for yourself: labs.tageswoche.ch/budget/budget.html
@QuantumLicht I found the problem. I had to change the name of the function to animateDiv, maybe in Chrome there is a collision between animate (jQuery function) and the specific animateDiv function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.