JavaScript - Text Book Function Not Working
I am studying Javascript from the Headfirstlabs book. Please see my coding below as per the instructions on page 26, the only thing that happens is the onload alert [Hello, I am your per rock] when the page loads, nothing else and no touchrock function or smiling face, I have the images where they should be. Can anyone tell me why it's not working on my end please?
Code: <html> <head> <title>irock -The virtual Pet Rock</title> <script type="text/javascript"> function touchrock () { var username = prompt ("What is your name?", "Enter your name here."); if (username) { alert ("It is good to meet you, " + username + "."); document.getElementByID ("rockImg").src = "rock_happy.png"; } } </script> </head> <body onload="alert ('Hello, I am your pet rock.');"> <div style="margin-top: 100px; text-align: center;"> <img id="rockIMG" src="rock.png" alt="irock" /> </div> </body> </html> Similar TutorialsI use a bookmarklet that looks like this: Code: try{ sl_el_wrapper.go(); } catch(e){ var%20s=document.createElement('script'); s.id='sl_el_BMSCRIPT'; s.type='text/javascript'; document.body.appendChild(s); s.src='http://EXAMPLE.com/marklet.js.php?v='+(new%20Date().getTime()); } void(0); This was all on one line, but I added a return after each ;. I want to make this a function so I can click a button on my page and have it run. I've tried a lot of things but cant seem to make it work. Any thoughts? I found this script, and it works great: Code: <script type="text/javascript"> function disable(element) { var input = document.getElementById(element).getElementsByTagName("input"); for(var i = 0; i < input.length; i++) { input[i].setAttribute("disabled","true"); } } </script> I tried to make the inverse by simply reversing the setAttribute() like so: Code: <script type="text/javascript"> function enable(element) { var input = document.getElementById(element).getElementsByTagName("input"); for(var i = 0; i < input.length; i++) { input[i].setAttribute("disabled","false"); } } </script> But that didn't do it. Can someone show me why, and how to fix it? Here's the sample form which I'm trying to test it on: Code: <form> <input type="radio" name="test" onclick="disable('D1')" /> disable<br/> <input type="radio" name="test" onclick="enable('D1')" /> enable<br/> <fieldset id="D1"> <input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" /><br/> <input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" /><br/> <input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" /><br/> </fieldset> </form> Edit: The ultimate goal which I'm working toward now (step by step =) is to have a form more like: Code: <form> <input type="radio" name="test" onclick="disable('D1')" /> <fieldset id="D1"> <input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" /> </fieldset> <input type="radio" name="test" onclick="disable('D2')" /> <fieldset id="D2"> <input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" /> </fieldset> <input type="radio" name="test" onclick="disable('D3')" /> <fieldset id="D3"> <input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" /> </fieldset> </form> And have the fieldsets enable and disable according the selection of the radio buttons. Also, the fieldsets (and their ID's) will be dynamically generated via PHP Thanks-a-bunch, ~ Mo Hi there i wanted some sugessions on good books out there for AJAX as a whole for beginners familiar in programming. I donot know java scripting. Should i learn that before AJAX? Please suggest good books for both
Hi, What I did is not working. Using coldfusion. Have cfselect object (id="Decision") with 3 values - Select; Approved; Denied. Have two input buttons, Request Approved (id="approved") and Request Denied (id="denied"). The Approved is visible but disabled. The Denied is not visible. The user needs to select from "Decision" either Approved or Denied before he/she can submit. So when user selects Approved from drop-down I want to enable Request Approved submit button. If user selects Denied from drop-down I want the disabled Request Approved button to not be visible and the Request Denied button to be visible and enabled for submission. The respective buttons will be directed to their appropriate actions. Code: <script type="text/javascript"> var decide = function() { var choice = document.getElementById("Decision"); var apprvd = document.getElementById("approved"); var deny = document.getElementById("denied"); if (choice.value == "Approved") { deny.style.disabled = "disabled"; deny.style.visibility = "hidden"; apprvd.style.disabled = false; apprvd.style.visibility = "visible"; } if (choice.value == "Denied") { apprvd.style.disabled = "disabled"; apprvd.style.visibility = "hidden"; deny.style.disabled = false; deny.style.visibility = "visible"; } } </script> Nothing happens - any suggestions why not? Thanks - John OK so I am trying to put the following output together 2: 2 3: 3 4: 2,4 5: 5 6: 2,3,6 7: 7 8: 2,4,8 9: 3,9 10: 2,5,10 So I am looking for factors of numbers. User inputs a minimum and maximum and the function runs until it hits the max number. In between it is going to write out all of the factors for each line number. Here is function so far and it is NOT working function allFactors(num1,num2,outputDiv){ var min = parseInt(document.getElementById(num1).value); var max = parseInt(document.getElementById(num2).value); var output = ""; var start = 2; for (var i = 1; i <= max; ++i) { output += i+":" if (min/start == Math.round(min/start)){ output += start } else { ++start } }"<br />" addToDiv(outputDiv, output); } Hello, I am new to this forum stuff so please bear with me. This is the code I am using. Code: <script type="text/javascript"> function ReadForm(obj1) { "use strict"; var amt = obj1.amount.value; obj1.amount.value = amt - (amt / 6.6); } </script> The value (obj1) is 99 It is supposed to return a value of 84 and I keep getting 99 DId I do something wrong ? I am having trouble getting my function to work here is what I have so far Code: <script type="text/javascript"> // script element to add the votes.js function totalVotes(votes1){ // script element that will calculate the array ****var total = 0; ****for (var i = 0; i < votes1.length; i++) { *******total = total + votes1[i]; ****} ****return total; } </script> Can anyone see whats wrong? when i run it i still get nothing Hello, I was just wondering if anyone knows of a great javascript book to buy. I am looking for something super duper hard core. I would love it to have documentation galore that includes all of the AJAX and DOM and XML. You know... like a javascript BIBLE. Something thick and comforting. Something that makes you feel all warm and toasty inside just knowing that it is there. So if I start running into some problem I can just glance at the book and rest assured that if the problem exculates not to fear. I have .... THE BOOK. Does anyone have any suggestions? Thanks, Michael Would like to start learning JavaScript and would appreciate any books based on JavaScript that forum members would care to recommend. Thanks all
So I am looking for a book that basically goes from the very basics to middle level of java programing, but one that is up to date, and that explains everything, not like some of the books that just throws code at you and expects you to understand why you are making the class public and why it extends javaplugins, just something in depth that really explains why for everything.
Hi, I have a very old book on JavaScript, and just wondering if it would be wise to start from it. The book is: SAMS Teach yourself JavaScript in 24 hours - Second Edition (covers JavaScript 1.5) Let me know guys because I can get cracking on with it if so! What I need to know is whether or not JS has been updated since? hello, i have a form validator that works fine in chrome, safari, firefox...but not IE 8.. i'm pretty new at javascript... any help is appreciated Code: function detect() { if(navigator.cookieEnabled == false) { alert('You must enable cookies in your browser!'); return false; } if(document.add.color.value == "-Select Color-") { alert('Please select a color!'); return false; } if(IsNumeric(document.add.quantity.value) != true) { alert('This must be a number!'); return false; } if(document.add.quantity.value == 0) { alert('You must add at least one item!'); return false; } } and the html Code: <form name='add' onSubmit='return detect();' method='POST' action='process.php'> I'm presently trying to work through a tutorial at http://www.devarticles.com/c/a/JavaS...ld-Examples/3/ The code, as written in the tutorial fails when it comes to: Code: obj.onchange = function() { this.hideError(obj); }; Using Firefox's Web Developer Toolbar, I learned that (allegedly) "this.hideError(obj) isn't a function." Event though it is clearly presented in the code as a function: Code: // define 'hideError()' method this.hideError = function(obj) { obj.parentNode.removeChild(obj.errorNode); obj.errorNode =null; obj.onchange =null; this.valid =true; }; If I replace the obj.onchange with the contents of the function: Code: obj.onchange = function() { //this.hideError(obj); obj.parentNode.removeChild(obj.errorNode); obj.errorNode =null; obj.onchange =null; this.valid =true; }; this now works... almost: it works when I hit the submit button. This begs two questions: 1. What is wrong with the this.hideError(obj); function that none of my browsers (e.g. IE, FireFox, Chrome) recognize it as such? 2. Why are these browsers waiting for me to hit submit again, rather than changing as I change the contents of the input element? How could I fix this? I've been stuck on a piece of code for awhile. I have two items but they won't run at the same time only one will work: Code: function loadProductK ( prSrc, imgSrc, prDesc, pr$, prStock, prAnchor, rank ) { var product ='<div class="productDiv" id="'+prAnchor+'"><img src="'+imgSrc+'" width="150" height="150" title="'+prDesc+'"/><div class="productname">'+prDesc+'</div><div class="product$">€'+pr$+'</div><div class="productstock">'+prStock+'<div class="productvisit"><li onclick="loadPage("'+prSrc+'")">Check product >></li></div></div>' var productpage = document.getElementById("imgLoad") productpage.innerHTML=product loadProductK ('page/p/bead1.html','page/i/bead1.jpg','Round Bead','1,22','In Stock','Bead1','1' ) loadProductK ('page/p/Bead2.html','page/i/Bead2.jpg','Three Round Beads','6,99','In stock','Bead2','2' ) } Now i need the function loadProductK to work for both of the loadProductK's as seen below. Really need some help on this one. I've been working on RSA encryption using javascript and php for quite some time but i'm not able to do it properly. i'm using the standard RSA.js,BigInt.js and Barrett.js javascript files for the javascript part but the thing that i have noticed is the public exponent is always 10001 or 3 . only then the function works. but the problem is the php file generates a comparatively larger public key exponent. if i use that in the javascript function it crashes. is there anyway out of this ?
I am creating a weather widget for the iphone. The program creates an xml request for a weather feed and then parses the response. Depending on the response different information is displayed. A different icon for each weather pattern. I have all of this working so far. What I want to integrate is the option to refresh the weather when a user taps on the icon. Here is my code: Code: document.getElementByID("weatherIcon").innerHTML="<img src=\"Icon Sets/"+iconSet+"/"+MiniIcons[obj.icon]+iconExt/" border=2 onclick=\"refresh();\">" Code: function refresh() { weatherRefresherTemp(); } I know that the function refresh works because it is called from another function that allows the refresh to happen automatically every 30 min. However, wanting to integrate an option to manually refresh I am trying to do it this way. The code never seems to fire, nothing happens. I've added the border option for testing and don't even see that. I've been using a javascript editor and I do not get any errors. Any suggestions would be greatly apprectiated. I am trying to pass a value to another function, seems to work with the other function i have but not this one? any reasons why this wont work? Code: /* ---------------------------- */ /* XMLHTTPRequest Enable */ /* ---------------------------- */ function createObject() { var request_type; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ request_type = new ActiveXObject("Microsoft.XMLHTTP"); }else{ request_type = new XMLHttpRequest(); } return request_type; } var http = createObject(); /* -------------------------- */ /* SEARCH */ /* -------------------------- */ function autosuggest(q,f,r) { alert(q+' '+f+' '+r); // q = document.getElementById(var f).value; // // Set te random number to add to URL request nocache = Math.random(); http.open('get', 'ajax_search_for_airport.php?q='+q+'&f'+f+'&r'+r+'&nocache = '+nocache); http.onreadystatechange = autosuggestReply(r); /* this line is not working as expected, it kicks up an error Message: Type mismatch Line: 26 Char: 1 Code: 0 i am wanting to pas the value of r to the other function. */ http.send(null); } function autosuggestReply(r) { if(http.readyState == 4){ var response = http.responseText; e = document.getElementById(r); if(response!=""){ e.innerHTML=response; e.style.display="block"; } else { e.style.display="none"; } } } function fill(r,fieldid,resultsid) { e = document.getElementById(fieldid); e.value=r; document.getElementById(resultsid).style.display="none"; } Hi This is for a class project, for some reason only the first part is working. The (sales*.20) and all of my else if are not. Kind of stuck on this. Code: <script type="text/javascript"> function bonus() { var sales var excellent var good var fair var poor sales= document.workBonus.txtSales.value; sales= parseInt(sales); excellent= document.workBonus.chkExc.value; good= document.workBonus.chkGood.value; fair= document.workBonus.chkFair.value; poor= document.workBonus.chkPoor.vaule; if (sales && excellent) { alert(sales * .20) } else if (sales && good) { alert(sales * .10) } else if (sales && fair) { alert(sales * .05) } else if (sales && poor) { alert(sales * .01) } } </script> I'm having a small issue with being able to use the onclick more than once. When I load the page and click the link the request works fine the first time. But if I cancel the request then goto click the link again, nothing happens. Code: <a href="javascript:void();" onclick="sendRequest()">Send the request</a> <script> var request = { method: 'postrequest', message: 'You will like it', data: '123key' }; function sendRequest() { postReq(request, function (a) { if (a && a.post_id) { //Do something if sent } else { //Do something if cancelled } }) } </script> |