Javascript function and combined variables, help appreciated.

Discussion in 'JavaScript' started by kigoobe, Dec 25, 2007.

  1. #1
    Well friends, I need some help, so here I am. I want to move the following code as a javascript function -

     
    <script type="text/javascript">  
            <!--
            var inter_<?=$cid?> = document.getElementById('chocQnty_<?=$cid?>');
            if (inter_<?=$cid?> && inter_<?=$cid?>.value) {
                var cqnty_<?=$cid?> = inter_<?=$cid?>.value;
            } else {
                var cqnty_<?=$cid?> = 0;
            }                  
            chocArray[<?=$countNo?>] = cqnty_<?=$cid?> + "~" + <?=$cprix?>;
            ajaxArr[<?=$countNo?>] = <?=$cid?> + "~" + cqnty_<?=$cid?>;
            subChoc('chocQnty_<?=$cid?>','totale_<?=$cid?>',<?=$cprix?>,chocArray,<?=$countNo?>,"",<?=$cid?>,ajaxArr);
     //-->      
     </script>
    
    Code (markup):
    As you can see, I need mixed / combined variables here, like inter_<?=$cid?> (I have used php here, since the javascript is a part of the main page, till now).

    However, I want to move that to an external file, and tried doing something like -
    function onloadEvents(cid,countNo,cprix) {
       var inter_+cid = document.getElementById('chocQnty_'+cid);
    }
    Code (markup):
    It's not working. Wondering how can I get an equivalent of inter_<?=$cid?> in this function, where the value of $cid will come as a function argument.

    Any help will be appreciated. Thanks. :)
    Merry christmass to all :)
     
    kigoobe, Dec 25, 2007 IP
  2. locdev

    locdev Active Member

    Messages:
    171
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hi
    try this code:

       <!--
    //this is in external include file
    function funcName(cid,cprix,countNo){
            var inter = document.getElementById('chocQnty_'+cid);
            if (inter && inter.value) {
                var cqnty = inter.value;
            } else {
                var cqnty = 0;
            }                  
            chocArray[countNo] = cqnty+ "~" + cprix;
            ajaxArr[countNo] = cid + "~" + cqnty;
            subChoc('chocQnty'+cid,'totale_'+cid,cprix,chocArray,countNo,"",cid,ajaxArr);
    }
    
    Code (markup):
    this is on the page
    replace the old code:
    
    <!--
    funcName('<?=$cid?>','<?=$cprix?>',<?=$countNo?>);
     //-->      
    
    Code (markup):
    Hope I didn't miss something
    Thanks
     
    locdev, Dec 25, 2007 IP
  3. kigoobe

    kigoobe Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    locdev, thanks a lot for your reply. Yeah, that's perfect, everything is working perfectly now :)
     
    kigoobe, Dec 25, 2007 IP