JavaScript - Subtracting Won't Work
Code:
<html> <script type="text/javascript"> var x=200; function update() { document.getElementById('health').innerHTML=x; } function attack() { x=x-10; } if (x<10) { document.write("You Won") } </script> <body onload="update()"> <div id="health"> </div> <input type="button" value="Attack" onclick="attack()" /> </body> </html> I don't see that x loses 10 what's the problem. Similar TutorialsHi All, I need help on how to subtract totalgift if my condition of donateamount is lesser than the totalgift trigger. When the field is selected and the DonateAmount is trigger, I need to subtract the entered amount on the field selected to the TotalGift and put the field selected to zero "0" and TotalGift to original amount which is less than the amount entered on the field selected. I have some code that I created but it doesn't work. My initial javascript to calculate all the total is below assuming that DonateAmount is already selected = 25: Code: /* this function works fine with the set of code*/ function CalculateTotal(donate_amount_other) { var tota = document.getElementById('a').value; var totb = document.getElementById('b').value; var totc = document.getElementById('c').value; var totd = document.getElementById('d').value; var tote = document.getElementById('e').value; var totf = document.getElementById('f').value; var totg = document.getElementById('g').value; var toth = document.getElementById('h').value; var toti = document.getElementById('i').value; var totj = document.getElementById('j').value; var totk = document.getElementById('k').value; var totl = document.getElementById('l').value; var totm = document.getElementById('m').value; var totn = document.getElementById('n').value; var toto = document.getElementById('o').value; var totp = document.getElementById('p').value; var totq = document.getElementById('q').value; var totr = document.getElementById('r').value; var TotC = document.getElementById('TotalC').value; TotC = Number(tota) + Number(totb) + Number(totc) + Number(totd) + Number(tote) + Number(totf) + Number(totg) + Number(toth) + Number(toti) + Number(totj) + Number(totk) + Number(totl) + Number(totm) + Number(totn) + Number(toto) + Number(totp) + Number(totq) + Number(totr); document.getElementById('TotalC').value = TotC.toFixed(2); if (isNaN(TotC)) { // if grandTotal is not a number alert ("You must enter numbers 0-9 only in the boxes"); document.getElementById('TotalC').value = "ERROR"; // or perhaps value = "ERROR!" return false; } /* the condition below triggers only when the toti - totr fields are entered less than 48 but still need to subtract the total if TotalGift is greater than DonateAmount given */ if (toti >0 && toti <48 ) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('i').value = "0"; // or perhaps value = "ERROR!" return false; } if (totj >0 && totj <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('j').value = "0"; // or perhaps value = "ERROR!" return false; } if (totk >0 && totk <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('k').value = "0"; // or perhaps value = "ERROR!" return false; } if (totl >0 && totl <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('l').value = "0"; // or perhaps value = "ERROR!" return false; } if (totm >0 && totm <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('m').value = "0"; // or perhaps value = "ERROR!" return false; } if (totn >0 && totn <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('n').value = "0"; // or perhaps value = "ERROR!" return false; } if (toto >0 && toto <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('o').value = "0"; // or perhaps value = "ERROR!" return false; } if (totp >0 && totp <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('p').value = "0"; // or perhaps value = "ERROR!" return false; } if (totq >0 && totq <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('q').value = "0"; // or perhaps value = "ERROR!" return false; } if (totr >0 && totr <48) { alert ("All donations in this section must be $48 minimum (processing fee may apply) - please revise your entry to avoid any processing errors"); document.getElementById('r').value = "0"; // or perhaps value = "ERROR!" return false; } /* The code below works if I only check the TotalGift is greater than DonateAmount but will cannot do subtraction to the TotalGift and the entered amount. */ if ((DonateText < TotC)) { alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.'); //add if here for all fields focus document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!" document.getElementById('TotalC').focus(); return false; } } Below is what I am trying to do but it doesn't work. If the first input field is selected and user enter amount greater than the TotalGift, alert will pop-up then and the amount entered by the user automatically added to the TotalGift. So what I wanted to do is to subtract the amount entered to the TotalGift so it will go back to the original amount before the addition occurs. I know it is possible but I just can't get the right code. Can you please help? I need to make this done as soon as possible, please!!!!! Code: if ((DonateText < TotC) && (document.getElementById('a').value)) { alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.'); //add if here for all fields focus if (TotC = "0.00") { document.getElementById('a').value=0; document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!" } else { TotC = TotC - document.getElementById('a').value; document.getElementById('tota').value = "0"; document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!" document.getElementById('TotalC').focus(); return false; } } if ((DonateText < TotC) && (document.getElementById('b').value)) { alert('Your total designated amount of $' + TotC + ' is more than your donation amount of $' + DonateText + '. Please revise your entry to avoid any processing errors.'); if (TotC = "0.00") { document.getElementById('b').value=0; document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!" } else { TotC = TotC - document.getElementById('b').value; document.getElementById('totb').value = "0"; document.getElementById('TotalC').value = TotC; // or perhaps value = "ERROR!" document.getElementById('TotalC').focus(); return false; } } /* so on My html code below which I have 18 input fields but all of them only optional wherever the user wants to input number. I will only include some here. Code below works, please ignore any missing code. I just need the calculation code to work. PHP Code: <label>Donation Amount: $</label> <input name="TotalDonate" id="TotalDonate" type="text" maxlength="10" readonly="true" onClick="CalculateTotal(this.value,99);" /> <label for="agencysupport" style="font-weight:bold">C. Agency/Program Support ($48 MINIMUM PER CHOICE)</label> <BR /> <p style="color:#000000">My gift is designated to one or more specific agencies or programs. Processing fee may apply. </p> <p style="color:#FF0000">(Note: Some Agency Names may be indented on the next line due to space allowed. Choosing either one of the Agency Names will give you the same code.)</p> <table> <tr> <td> <select id="designation_list1" name="designation_list1" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list1']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="i" name="i" class="text" type="text" value="<?php safeEcho($form['i'])?>" style="width:90px;" onChange="CalculateTotal()"/> <?php helper_error('i');?> </td> </tr> <tr> <td> <select id="designation_list2" name="designation_list2" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list2']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="j" name="j" class="text" type="text" value="<?php safeEcho($form['j'])?>" style="width:90px;" onChange="CalculateTotal()"/> <?php helper_error('j');?> </td> </tr> <tr> <td> <select id="designation_list3" name="designation_list3" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list3']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="k" name="k" class="text" type="text" value="<?php safeEcho($form['k'])?>" style="width:90px;" onChange="CalculateTotal()"/> <?php helper_error('k');?> </td> </tr> <tr> <td> <select id="designation_list4" name="designation_list4" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list4']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="l" name="l" class="text" type="text" value="<?php safeEcho ($form['l'])?>" style="width:90px;" onChange="CalculateTotal()"/> <?php helper_error('l');?> </td> </tr> <tr> <td> <select id="designation_list5" name="designation_list5" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list5']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="m" name="m" class="text" type="text" value="<?php safeEcho($form['m'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('m');?> </td> </tr> <tr> <td> <select id="designation_list6" name="designation_list6" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list6']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="n" name="n" class="text" type="text" value="<?php safeEcho($form['n'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('n');?> </td> </tr> <tr> <td> <select id="designation_list7" name="designation_list7" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list7']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="o" name="o" class="text" type="text" value="<?php safeEcho($form['o'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('o');?> </td> </tr> <tr> <td> <select id="designation_list8" name="designation_list8" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list8']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="p" name="p" class="text" type="text" value="<?php safeEcho($form['p'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('p');?> </td> </tr> <tr> <td> <select id="designation_list9" name="designation_list9" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list9']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="q" name="q" class="text" type="text" value="<?php safeEcho($form['q'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('q');?> </td> </tr> <tr> <td> <select id="designation_list10" name="designation_list10" size="1" style="overflow-x:scroll; width:380px; "> <option>Click to select...</option> <?php foreach ($designation_list as $descode => $prompt) : ?> <option <?php echo helper_selected($descode == $form['designation_list10']); ?> value="<?php echo $prompt; ?>" ><?php safeEcho($descode); ?> </option> <?php endforeach; ?> </select> </td> <td> $<input id="r" name="r" class="text" type="text" value="<?php safeEcho($form['r'])?>" style="width:90px;" onChange="CalculateTotal();"/> <?php helper_error('r');?> </td> </tr> <tr> <td style="text-align:right; font-size:14px"><b>TOTAL GIFT(S) DESIGNATED (A-C)</b><BR /> (Must not exceed donation amount)</td> <td>$<input name="TotalC" id="TotalC" type="text" maxlength="10" value="<?php echo $TotalC; ?>" readonly="true" onChange="CalculateTotal(this.value,99);" style="width:90px" /></td> </tr> </table> Please, I really need to make this work today. Hope you can help me please!!!! Thanks in advance. Well, I'm a beginner to coding, so I need some help. I'm trying to create a short, html/javascript mario text game. Now, in the beginning, he fights a goomba. He has a health and energy number as variables: Code: <p><img src="http://img253.imageshack.us/img253/9202/mariodisplayidlefront.png" align="middle" hspace=20> <font size=3 color="red"> Health: <script language= "javascript"> var mariohealth = '20'; document.write(mariohealth); </script> </font> <br><font size=3 color="green"> Energy: <script language= "javascript"> var marioenergy = '20'; document.write(marioenergy); </script> </align> </font> And the goomba has health: Code: <p><br><img src="http://img23.imageshack.us/img23/9438/goombafrontdisplay.png" align="middle" hspace=20> <font size=3 color="red"> Health: <script language= "javascript"> var goombahealth = '10'; document.write(goombahealth); </script> </font> And then, there will be different buttons for different attacks. However, the only one created is "Punch", with this code: <FORM> <INPUT type="button" value="Punch" name="Mario Punch" onClick="?"> </FORM> What would I put in the onClick function to make it subtract 4 health from the goomba and 2 health from Mario Putting some javascript into a pdf file and can't get this to work out right. Basic setup: Field X = checkbox; Field Y = numeric input field; Here's what should happen (increment is toggled): Y has a value. When X is checked, Y is increased by 5. When X is unchecked, Y goes back to its original, unmodified value. Here's what really happens (increment is subtracted): Y has a value. When X is checked, Y is reduced by 5. When X is unchecked, Y is reduced by 5 again. Anytime X is clicked on, Y is reduced by 5 yet again. Here's the code: Code: function check_box(X, Y){ if(X.checked == true){ Y.value += 5; } else { Y.value -= 5; } Any help or advice is greatly appreciated. Hello all, I made a fade script that will fade any element in or out. Works great on all browser I've tested but IE 7. With IE I have only tested this will IE 8 and IE 7. IE 7 the effect doesn't work. No error message or anything. I'm unsure what to do from here. I was hoping I could find some help here. Code: function fade(obj, duration, toggle) { steps = 1000; elem = document.getElementById(obj); function fadeIn() { for(var i = 0; i <= 1; i+=(1/steps)) { setTimeout("elem.style.opacity = "+ i +"", i * duration); setTimeout("elem.style.filter='alpha(opacity="+ i * 102 +")'", i * duration); } } function fadeOut() { for(var i = 0; i <= 1; i+=(1/steps)) { setTimeout("elem.style.opacity = "+ (1-i) +"", i * duration); setTimeout("elem.style.filter='alpha(opacity="+ (1-i) * 102 +")'", i * duration); } } /* One for Fade in and anything will be fade out*/ if(toggle == 1) { fadeIn(); } else { fadeOut(); } } Thanks, Jon W hello, I found this script : http://roshanbh.com.np/2008/06/accor...ng-jquery.html I implemted this into my website http://test.tamarawobben.nl but as you can see it don't work. Can anyone help me figure out why the script does not work. Roelof Hi I'm doing some js online tutorials and am stuck at this piece of code. It's supposed to be a carousel that loops. It's supposed to have the main image in the middle and images on either side of it. Something similar to www.atlanticrecords.com. But my code doesn't make it slide. I'm a complete novice to javascript and am desparate for this to work. This is the code if anyone would like to show me my mistakes. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Interactive Carousel</title> <style type="text/css"> #feature_holder{position:relative; top:150px; overflow:hidden; height:400px; width:100%;} .feature{position:absolute; width:1000px; height:400px; top:0; z-index:1;} .arrow_right{right:20px;} .arrow_left{left:20px;} .arrow{height: 70px; width:70px; z-index:2; top:160px; position:absolute; background:#000;} #feature_1{background: #666666;} #feature_2{background: #225566;} #feature_3{background: #006600;} #feature_4{background: #ff0066;} #feature_5{background: #00ff00;} #feature_6{background: #0380ff;} </style> </head> <body> <div id="feature_holder"> <div class="arrow arrow_left"></div> <div class="arrow arrow_right"></div> <div class="feature" id="feature_1"></div> <div class="feature" id="feature_2"></div> <div class="feature" id="feature_3"></div> <div class="feature" id="feature_4"></div> <div class="feature" id="feature_5"></div> <div class="feature" id="feature_6"></div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script> var BrowserWidth var FeatureCount var CurrentFeature = 1 var NextFeature var NextNextFeature var PrevFeature var PrevPrevFeature $(document).ready (function(){ setFeatureCount() getBrowserWidth() setClicks() setNextPrev() setFeaturePosition() }); setFeatureCount = function(){ FeatureCount = $('#feature_holder').children('.feature').length; } getBrowserWidth = function(){ BrowserWidth = $(window).width(); } setClicks = function(){ $('#feature_holder').children('.arrow_left').click(function(){ SlideInFeature(true); }); $('#feature_holder').children('.arrow_right').click(function(){ SlideInFeature(false); }); } setNextPrev = function(){ PrevFeature = (CurrentFeature-1); PrevPrevFeature = (CurrentFeature-2); NextFeature = (CurrentFeature+1); NextNextFeature = (CurrentFeature+2); if (CurrentFeature == 1){ PrevFeature = FeatureCount;} setFeaturePosition = function(){ $('#feature_holder').children('.feature').css('left', -9999); var CurrentPositioning = (BrowserWidth/2) - 500; $('#feature_'+CurrentFeature).css('left', CurrentPositioning); var NextPositioning = (BrowserWidth/2) + 500; $('#feature_' +NextFeature).css('left', NextPositioning); var PrevPositioning = (BrowserWidth/2) - 1500; $('#feature_'+PrevFeature).css('left', PrevPositioning); }} </script> </body> </html> Thanks Can somebody give me a bit of help with the below code. The problem is that it works fine for the first pop up box but not for the other(s). I think it has something to do with the script using the getelementbyid. Can anybody help? Code: <script type="text/javascript"> var appearance = "notShown" function show() { if (appearance == "notShown") { document.getElementById('P1').style.display = "inline"; appearance = "shown"; } } function remove() { document.getElementById('P1').style.display = "none"; appearance = "notShown"; } </script> <style type="text/css"> p#P1 { width: 319px; height: 169px; display:none; background-color: grey; position:absolute;z-index:2; } p#P1 b { padding: 10px; float: left; } p#P1 span { width: 300px; float: left; padding: 10px;} button.close {float:right; margin: 5px; } </style> <a href="#" onclick="show()">box 1</a> <p id="P1"><b>Lorem Ipsum</b><button type="button" class="close" onclick="remove()">X</button> <span>Lorem ipsum ut volumus antiopam pericula vix, per nulla oblique alienum ad. No ullum convenire eos. Sit tota iusto ut, ex mentitum voluptatum inciderint est. Ex dico idque argumentum eam, ei elit meliore definiebas per, nam soleat aliquando ad.</span> </p> <br /><br /><br /><br /><br /><br /> <a href="#" onclick="show()">box 2</a> <p id="P1"><b>Lorem Ipsum</b><button type="button" class="close" onclick="remove()">X</button> <span>Lorem ipsum ut volumus antiopam pericula vix, per nulla oblique alienum ad. No ullum convenire eos. Sit tota iusto ut, ex mentitum voluptatum inciderint est. Ex dico idque argumentum eam, ei elit meliore definiebas per, nam soleat aliquando ad.</span> Hi there! I'm completely new to javascript coding, but have suddenly found myself in need of using it. here's a little background (helps me keep track of where I am in my question). I've been "playing" with php for quite a while, and I decided I wanted to see how hard it would be to code a fairly simple forum. So far, everything is working out with very little need for bug fixes and some efficiency suggestins from a friend. I'm currently building the registration page, and wanted to have it compare the password and verification while the form is being filled. I found a code to do it after a few searches, but evidently I am missing something about how it works. This is the code I found: Code: <script type='text/javascript'> function comparePasswords() { if(document.getElementById('pass1').value != document.getElementById('pass2').value) { $('passwordShow').innerHTML = 'Passwords do not match'; } else { $('passwordShow').innerHTML = ''; } } </script> This is the full code for the form I am trying to use it in: Code: <?php echo "<script type='text/javascript'>\n function comparePasswords()\n {\n if(document.getElementById"; echo "('pass1').value != document.getElementById('pass2').value)\n {\n $('passwordShow').innerHTML"; echo " = 'Passwords do not match';\n }\n else\n {\n $('passwordShow').innerHTML = '';\n"; echo " }\n }\n</script> "; echo "<form action='index.php?pg=verify' method='post'>"; echo "<table align='center' style='color: #C0C0C0'>"; echo "<tr><td>Username:</td><td><input type='text' name='user' /></td></tr>"; echo "<tr><td>Password:</td><td><input type='password' name='pass1' /></td></tr>"; echo "<tr><td>Re-enter Password</td><td><input type='password' name='pass2' onkeyup='comparePasswords();' /></td></tr>"; echo "<tr><td>Email<br />(used for password recovery)</td><td><input type='text' name='email' /></td></tr>"; require ('questions.php'); echo "<input type='hidden' name='source' value='reg' />"; echo "<br><center><input type='submit'/></form>"; ?> What am I not understanding about this? So i got this to work once, but i can't get it to work now. I am trying to make it so when i clicked a radio button, i get the description of each object i click. Please help!!! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <head> <script type="text/javascript"> var vegetarian="lots of mushrooms" var hawaiian="overloaded with juicy" var meatlovers="loads of pepperoni" </script> </head> <body> <form name="pizzaList" action="" method="get"> <p> Click the buttons for a description of each pizza.</p> <p> <input type="radio" name="pizzas" onclick="document.pizzaList.pizzaDesc.value=vegetarian" />Vegetarian <input type="radio" name="pizzas" onclick="document.pizzaList.pizzaDesc.value=hawaiian" />Hawaiian <input type="radio" name="pizzas" onclick="document.pizzaList.pizzaDesc.value=meatlovers" />MeatLovers </p> </form> <p> <textarea name="pizzaDesc"></textarea></p> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> </html> HI i have a form and submit bottom in my page . I've wrote a function that check the page element's value before submitting the form. my problem is this i couldn’t stop submitting the form before it has checked . Actually I want before the form submitted it has checked and if there was no problem then the function return true and submitted and if there was any problem, the function return false and the form stopped submitting I ‘ve added a function to the onclick of submit button and it call a function that check the values thanks Hello again, I have written the following code: <script language="Javascript"> var win=0 </script> <Form name="Form"> <Input type="button" name="button" value="Click" onClick="Good()"> </form> <script language="Javascript"> function Good() { if (win=0) { alert('You win!') } else { alert("You lose!") } } </script> Based on the following, when I click the button it should say "You win!" as win=0 but it keeps saying "You lose!". Why is this happening? I know we've all seen those text fields where when you type in am HTML tag it changes color! Example: <div> blah blah blah blah </div> I know that there is no flash involved, so how does it work? So here is the story I paid for some to create a slideshow, and it works fine in the orginial html document but when i copy and paste the code into my html document the scroller to the left of the image stops moving up and down I put the working file at this address: http://rusty813.comuv.com/ my files at this address where i am: http://rusty813.comuv.com/sample2.html the guy i paid to do work refuses to help even those i advertised the price in my ad and he wanted me to give it to pay him more to tell which code to change but go ahead take a look at the coding thanks alot for your guys help I've been working for some time trying to get XMLHTTPRequests to work. Nothing doing. The code seems simple enough, but I can't duplicate it on my own. I have two pages on the same PC. Code to follow. Please help me see the problem: Page One - Originator <code> <html> <head> <title>Origination</title> <script type='text/javascript'> function ajaxRequest(){ var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE if (window.ActiveXObject){ //Test for support for ActiveXObject in IE //first (as XMLHttpRequest in IE7 is broken) for (var i=0; i<activexmodes.length; i++){ try{ return new ActiveXObject(activexmodes[i]); } catch(e){ //suppress error } } } else if (window.XMLHttpRequest) // if Mozilla, Safari etc return new XMLHttpRequest(); else return false; } //Usage: //new ajaxRequest() function getStuff() { var getdata = new ajaxRequest(); if (getdata.readyState==4){ if (getdata.status==200){ var htmldata=getdata.responseText var txtfields = htmldata.getElementsByTagName('input'); alert(txtfields); } else{ alert('An error has occured making the request'); } } getdata.open('POST', 'test.html', true); getdata.send(null); } </script> </head> <body> <h5>Origination</h5> <form <form action="file:///test.html" method="post" enctype="text/plain"> <input type='text' name='homepage' value=''/> <input type='button' name='getit' value='GetSome' onclick='getStuff()'/> </form> </body> </html> </code> Page Two - The Target Page: <code> <html> <head><title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/> <script type="text/javascript" src=" http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"/> <style type="text/css"> html {font-family: arial; font-size: 0.8em; } </style> </head> <body> <div id="wrapper"> <form action="file:///eyec.html" method="post" enctype="text/plain"> <input type='text' name='first' value='First Field'/> <input type='text' name='second' value='Second Field'/> <input type='text' name='third' value='Thrird Field'/> <input type='text' name='fourth value='Fourth Field'/> </form> </div> <!-- wrapper --> </body> </html> </code> I'd deeply appreciate correction. Thanks. Im new to javascript and trying to make a form validator, ive succeeded but its left me confussed as to setting variables. my original code was Code: function applyform() { var name = document.getElementById('mainname'); var lvl = document.getElementById('mainlvl'); var class = document.getElementById('mainclass'); var role = document.getElementById('mainrole'); var reason = document.getElementById('reason'); var age = document.getElementById('age'); var mic = document.getElementById('microphone'); var voice = document.getElementById('voice'); if(notempty(name, "Please enter your main characters name")){ if(dropselect(lvl, "Please select your characters level")){ if(dropselect(class, "Please select your characters class")){ if(dropselect(role, "Please select your characters role")){ if(notempty(reason, "Please tell us why you wish to join us")){ if(notempty(age, "Please state your age")){ if(notempty(mic, "Please state if you have a microphone")){ if(notempty(voice, "Please state if you will use voice communication during events")){ return true; } } } } } } } } return false; } using this stoped all my javascript functions working but i cant see any fault with it. I've changed the code now to Code: function applyform() { if(notempty(document.getElementById('mainname'), "Please enter your main characters name")){ if(dropselect(document.getElementById('mainlvl'), "Please select your characters level")){ if(dropselect(document.getElementById('mainclass'), "Please select your characters class")){ if(dropselect(document.getElementById('mainrole'), "Please select your characters role")){ if(notempty(document.getElementById('reason'), "Please tell us why you wish to join us")){ if(notempty(document.getElementById('age'), "Please state your age")){ if(notempty(document.getElementById('microphone'), "Please state if you have a microphone")){ if(notempty(document.getElementById('voice'), "Please state if you will use voice communication during events")){ return true; } } } } } } } } return false; } this works fine but i don't get why my first bit of code didnt work and id really appreciate someone explaining it to me. thanks in advance already solved
This is a script which has a window slide down on the page and a user can choose yes/no to continue. It works in all browsers other than IE...any help please? If you need more info, please let me know. Thanks. <script language="JavaScript" type="text/javascript"> <!-- var allowpop=1; function popWin(){ var ppl="popLayer";var objppl=findObj(ppl); if (objppl==null){return;}// if the layer does not exist, do nothing. var args=arguments,movetoX=parseInt(args[0]),movetoY=parseInt(args[1]),movespeed=parseInt(args[2]); var cycle=10,pxl=""; if(!document.layers){objppl=objppl.style;} if(objppl.tmofn!=null){clearTimeout(objppl.tmofn);} var pplcoordX=parseInt(objppl.left),pplcoordY=parseInt(objppl.top); var xX=movetoX,yY=movetoY;if((pplcoordX!=movetoX)||(pplcoordY!=movetoY)){ var moveX=((movetoX-pplcoordX)/movespeed),moveY=((movetoY-pplcoordY)/movespeed); moveX=(moveX>0)?Math.ceil(moveX):Math.floor(moveX);movetoX=pplcoordX+moveX; moveY=(moveY>0)?Math.ceil(moveY):Math.floor(moveY);movetoY=pplcoordY+moveY; if((parseInt(navigator.appVersion)>4||navigator.userAgent.indexOf("MSIE")>-1) && (!window.opera)) {pxl="px";} if (moveX!=0){eval("objppl.left='" + movetoX + pxl + "'");} if (moveY != 0) {eval("objppl.top = '" + movetoY + pxl + "'");} var sFunction = "popWin(" + xX + "," + yY + "," + movespeed+ ")"; objppl.tmofn = setTimeout(sFunction,cycle); } } function findObj(theObj, theDoc){ var p, i, foundObj; if(!theDoc) theDoc = document; if((p = theObj.indexOf("?")) > 0 && parent.frames.length) {theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p);} if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj; } function hideLayer(layername){ layer=findObj(layername); if(layer.style){layer=layer.style;} layer.visibility='hidden'; } // --> </script> Okay I'm trying to make a script where if I click on a link a div pops up. And if I click on another link another div pop ups up and the old div is closed. The script calls the function from the link in the html and sends an ID. I am confused as to why this doesn't work. Code: function controls(vtiles){/*ID of requested div*/ vtiles.style.ClassName='switchOn'; //change it's classname to something that's displayed var cap=document.getElementsByTagName('div');// get every div element for(i=0;i<cap.length;i++) // make some kina loop with the number of divs { var store=cap[i].ClassName; if(store=='switchOn' && cap.[i].id); { /*look for div's that are on (switchOn) and check to see if they have and ID*/ var check = cap.[i].id //store their IDs if(check!=vtiles){//check to see if the id is the one stored in Vtiles store=='switchP'//change the classname to switchP(off) } } } } Code: <div class="content"><a class="Atiles" href="#" onclick="controls(document.getElementById('Ankara'))">Ankara</a></div> <div id="Ankara" class="switchP"> Code: .switchP{ position:absolute; display:none; z-index:-1; left: 20px; top:40px; } .switchOn{ position:absolute; display:block; z-index:100; left: 20px; top:40px; width:200px; height:200px; } hey I'm a bit confused, I really am looking for some javascript tutorials so I don't have to keep asking for help. I am terrible at this. I want to get this functionality seen here http://davidlynch.org/js/maphilight/docs/demo_usa.html (image map areas highlighting on mouseover) to work on this here http://scratchitrich.com/test/ can anybody walk me through how to make this work? Thanks! |