Jump to content

changing javascript global variable inside function..


Recommended Posts

hello im trying to change a variable set outside of a function and calling the function with an onchange... i'm having problems getting the variable to change

 

 

 

 

<script type="text/javascript">


var price = Number('<?php echo $price; ?>');


function addtwo()
{

if(document.add.size.value == "2xl")
{

price = price + 2;
}
}

</script>

Why don't you parse the value with the onchange

 

onChange="addtwo('<?php echo $price; ?>');"

 

 

<script type="text/javascript">

 

function addtwo(p)

{

 

if(document.add.size.value == "2xl")

{

p = price;

price = price + 2;

}

}

 

Not tested it but I think that works better.

 

</script>

are you sure it's the variable? There's no reason the variable shouldn't be incremented.  I think the more likely problem is that the condition fails or the function fails to be called.  put an alert(price) into your function, outside of the condition.  If it fails to alert, your function is failing to be called.  If it does alert, put the alert inside your condition. If it fails to be called, your condition is failing.

 

 

 

 

okay the alert works... and it shows the incremented variable... but i guess i'm not displaying it right? here's my code

 

echo "<tr>";
echo "<td style='text-align:right;padding-right:10px;' id='certain'>Price:</td><td style='font-size:16px;padding-left:5px;color:#fff;'>$<script type='text/javascript'>document.write(price);</script></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='text-align:right;padding-right:10px;' id='certain'>Size:</td>
<td style='padding-left:5px;'>
    <select class='size' name='size' onChange=\"addtwo();\">
        <option value='s'>S</option>
        <option value='m'>M</option>
        <option value='l' SELECTED>L</option>
        <option value='xl'>XL</option>
        <option value='2xl'>2XL (Add $2.00)</option>
    
    </select>
</td>";
echo "</tr>";  

i did notice... with this function ...everytime you onchange it adds 2... i would not like to have it continue to add over and over in case someone selects it over and over.... as you can see in the html there are various sizes and if "2xl" is selected i would like the price to display 2 dollars more than the original price and if switched back then the price goes back as well....i'm thinking maybe the function needs to be redone...any suggestions?

well haku let me explain my thoughts... what is it about my question that is not understandable? is it the part where i use php to echo out the html?... listen... i come onto these forums because there are intelligent people who like helping people who don't understand programming at your genius level yet... it's not like my situation is complicated....

 

... i have a simple select with sizes that if you look closely... the XL size needs to add $2.00 to the price when selected....this seems like it could be accomplished simply but I can't figure it out...which brings me to the forum

 

i'm not asking anyone to write me a whole new working function or anything...i came here for ADVICE...like "yeah you could try doing this or this..." and if it's something I don't understand yet I can look it up....i appreciate everyones responses that gave their time and i appreciate the ones who chose to waste my time to not post...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.