JavaScript - If No Matches, Show Alert
hi,
I've been making a search box, which works as it should like this: Code: function searchLocations() { var input = document.getElementById('tb').value; if (!input) { alert("please enter a name"); } else { for (var j = 0; j < gmarkers.length; j++) { if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) { gmarkers[j].show(); myclick(j); } } } } but then if the user types a term that doesn't match, it does nothing. So I wanted an alert, which I figured would go like this: Code: function searchLocations() { var input = document.getElementById('tb').value; for (var j = 0; j < gmarkers.length; j++) { if (input.toLowerCase() == gmarkers[j].myname.toLowerCase()) { gmarkers[j].show(); myclick(j); } else alert("no results found"); } } or this: Code: function searchLocations() { var input = document.getElementById('tb').value; for (var j = 0; j < gmarkers.length; j++) { if (gmarkers[j].myname.toLowerCase() !== input.toLowerCase()) { alert("no results found"); } else { if (gmarkers[j].myname.toLowerCase() == input.toLowerCase()) { gmarkers[j].show(); myclick(j); } } } } but either way pops an alert, even if the search term is valid, and locks the page up. which is not quite what I was looking for. any suggestions? Similar TutorialsHi, I'm doing some experimenting. So this may look bad to javascript experts here. But I'm trying to learn. I have a header div that will show a larger image when the user mouses over each thumbnail image. I used jQuery to create this effect. The header div contains a button. Once the button is clicked, an alert will pop up to tell the user the artist's name of image. The button is wired to an ID of a paragraph. Problem is, my code is not working. I don't want to use "onclick" inline javascript (which is what I am using to call the showArtistname() function). I want unobtrusive javascript like jQuery is. I'm not sure how to do this. I don't know if I should use an array or if I'm even approaching this correctly. Well, it's not correct, because it's not working... Here is my jQuery code: Code: $(function(){ $("a:has(img.small)").mouseover(function(){ var bigImage= $(this).attr("href"); $("#heading").attr({src: bigImage}); return false; }); }); here is my javascript code: Code: function showArtistname(){ var a = document.getElementById("bluesails", "purplemountains", "bigsky", "nightlights", "fireysunset", "brilliantsunrise").innerHTML; switch(a) { case "bluesails": alert("Arthur MacKenzie") break case "purplemountains": alert("Maggie Laing") break case "bigsky": alert("Arthur MacKenzie") break case "nightlights": alert("Aria Soriano") break case "fireysunset": alert("Felix Buckley") break case "brilliantsunrise": alert("Felix Buckley") } } Here is the HTML: Code: <div class="container_12" id="_container"> <div class="grid_12" id="12_header" > <div class="hc_left_pic"> <images/01_md.jpg" id="heading" alt="Big Image." /> <div id="showImage" onclick="showArtistname()"></div> </a></div> </div> <div class="clear"></div> <div class="grid_4" id="artist_container1"> <a href="images/01_md.jpg"><img src="images/01_sm.jpg" width="100" height="100" class="small" alt="Small image Blue Sails" /></a> <p class="text" id="bluesails">Blue Sails</p> </div> <div class="grid_4" id="artist_container2"> <a href="images/02_md.jpg"><img src="images/02_sm.jpg" width="100" height="100" class="small" alt="Small Image Purple Mountains"/></a> <p class="text" id="purplemountains">Purple Mountains</p> </div> <div class="grid_4" id="artist_container3"> <a href="images/03_md.jpg"><img src="images/03_sm.jpg" width="100" height="100" class="small" alt="Small Image Big Sky"/></a> <p class="text" id="bigsky">Big Sky</p> </div> <div class="clear"></div> <div class="grid_4" id="artist_container4"> <a href="images/04_md.jpg"><img src="images/04_sm.jpg" width="100" height="100" class="small" alt="Small Image Night Lights" /></a> <p class="text" id="nightlights">Night Lights</p> </div> <div class="grid_4" id="artist_container5"> <a href="images/05_md.jpg"><img src="images/05_sm.jpg" width="100" height="100" class="small" alt="Small Product Image Firey Sunset"/></a> <p class="text" id="fireysunset">Firey Sunset</p> </div> <div class="grid_4" id="artist_container6"> <a href="images/06_md.jpg"><img src="images/06_sm.jpg" width="100" height="100" class="small" alt="Small Product Image Brilliant Sunrise"/></a> <p class="text" id="brilliantsunrise">Brilliant Sunrise</p> </div> Here's the CSS: Code: .container_12 .grid_4 { width: 274px; height: 370px; background-color:#ccc; border: 3px solid #999; padding-left:10px; padding-right:10px;:confused: padding-bottom:10px; } #showImage{ margin: -170px 20px 80px 700px; width: 176px; height: 48px; background:url(../images/showimage.jpg); position:relative; z-index:100; } .grid_4 img { position:relative; left:100px; top:20px; padding:0 0 60px 0; border:none; } .grid_4 p { position:relative; text-align:center; } p.text { font-family:Arial, Helvetica, sans-serif; font-size:.75em; color:#000; line-height:1.25em; font-weight:bold; } #12_header { display: inline; background-color:#e5e5e5; border: 3px solid #bfbfbf; height:225px; font-family:Arial, Helvetica, sans-serif; font-size:; color:#000; font-weight:bold; line-height:1.2em; } .hc_left_pic { float:left; margin-top:15px; background-color:#e5e5e5; border: 3px solid #bfbfbf; width:935px; height:250px; } I have an user table like this:- guid | username | password | firstname | lastname | location | emailad dress | userrole -----------------------------------+----------+----------------------------------+-----------+-----------+----------+-------- ------+--------------- 8024259764dc3e8ee0fb6f5.84107784 | james | 827ccb0eea8a706c4c34a16891f84e7b | james | bond | NY | ny@live .com | administrator 18689183644dc3e91571a364.71859328 | saty | 250cf8b51c773f3f8dc8b4be867a9a02 | saty | john | NY | hk@fd.c om | administrator 2644885344cecd6f2973b35.63257615 | admin | 21232f297a57a5a743894a0e4a801fc3 | System | Generated | | | administrator (3 rows) now my postgre query for delete the row .... $query = "delete from users where username!= 'admin' and guid='".$guid."'"; $result = pg_query($conn, $query); ?> <script type="text/javascript"> alert("Cannot delete this .\n It is system generated(s)."); </script> <?php (1)when I delete the user name one by one then delete occurs in my page userlist.php, I donot want to delete admin so i use username!= 'admin' in where condition as shown above. (2)now when I del any username(3 rows) from user table then alert occurs & it delete from userlist.php after that my page userlist.php is blank. Finaly when i refresh the page then my admin username seen.. when i use return true; function then only alert generate .. delete doesnot occurs ... Actauly i want:- (1)if user is not admin then it delete from userlist.php ... nd also i m continue on this page ... like when james and saty want to delte their acount ..as given in table. (2)if user is admin then alert generate nd i m continue on this page. i m tired now plz help me .... so can anyone put the best condition in my coding. Okay, this should be an easy one. I'm a JS newb, so be kind The code I have right now is an autosuggest feature, but currently, it only finds exact matches, not partials. I want it to find partials. I know the line of code I need to rewrite, but I can't figure out what JS function to use. For example, if a user types "pizza", i want "pepperoni pizza" to show up, not just things that start with "pizza". Code: // str is the string that the user is typing in // this.aNames is an array containing all the possible strings to match against // aList is the list of matches from aNames when compared to str autoCompleteDB.prototype.getMatches=function(str,aList,maxSize) { /* debug */ //alert(maxSize+"ok getmatches"); var ctr=0; for(var i in this.aNames) { if(this.aNames[i].toLowerCase().indexOf(str.toLowerCase())==0) /*THIS LINE NEEDS TO BE CHANGED*/ { aList.push(this.aNames[i]); ctr++; } if(ctr==(maxSize-1)) /* counter to limit no of matches to maxSize */ break; } }; How can I go about making it so JavaScript will not let a form submit unless a number entered into a field matches a pre-specified number? A guess.. Code: function validateForm() { if (document.forms["form"]["number"].value=="123") { submit} else {alert ("Cant submit because the number doesn't match."); return false; } } Hello everybody I'm working on a tiny personal project, that only needs a little push to be finished. As I'm not a skilled enough scripter, I had to google around, but without any result exactly same as mine. What I'd like to know, how can I search in the body for strings surrounded by other strings that signs that the string I want is between them? example: Code: I'm a part of the body :3 // <- not surrounded part of the body [cust]You want me in your array :)[/cust] // <- this one is surrounded I won't get into your array :( // <- this one is not, just like the most of the body I have no snippet that could help me. Thank you in advance Hi, I have this code: var finalsort="("+getvalue+","numvalue")\n" alert(finalsort) however, it is then alerted three seperate times (as it is in a for loop) but i would like this to be displayed in an alert: (david,5) (james,3) (tom,1) How would I do this, am I doing something wrong with the \n? thanks All, I have the following code: Code: <script type="text/javascript"> alert("This picture has received more then 10 down votes so it is now being deleted. You will be redirected back to the pictures page!"); </script> <? echo "<meta http-equiv=\"refresh\" content=\"2;url=http://website.com/pictures.php\">"; ?> This is just in the code. I thought it would just execute this but it doesn't. I can't have it appear on a button event or an onload since this snipped gets executed with AJAX. How can I make it appear? Thanks in advance! I am validating the value in a text area onBlur. If the value is not good the alert box comes up twice. Is there a way to correct this? Code: <html> <head> <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1"> <title>Untitled</title> <script type="text/javascript"> function verify_score(val1) { var temp = document.getElementById(val1) if(temp.value == "AF"|| temp.value == "HF" || (temp.value >=0 && temp.value <=17)) {return} else{ alert("Value entered is not OK. Accetable values a 0 to 17, AF or HF") temp.focus() temp.select() } } </script> </head> <body> <div id="PageDiv" style="position:relative; min-height:100%; margin:auto; width:200px"> <form name="scoresheetform" action=""> <div id="Submitbutton" style="position:absolute; left:52px; top:87px; width:110px; height:33px; z-index:1"> <input type=submit name="Submitbutton" value="Submit Form"></div> <div style="position:absolute; left:9.822px; top:37px; width:45px; height:29px; z-index:2"> <input id="HomePlayer1Rnd6Pnts" name="HomePlayer1Rnd6Pnts" onBlur="verify_score('HomePlayer1Rnd6Pnts')" size=3></div> <div style="position:absolute; left:79.822px; top:37px; width:45px; height:29px; z-index:3"> <input id="HomePlayer1Rnd7Pnts" name="HomePlayer1Rnd7Pnts" onBlur="verify_score('HomePlayer1Rnd7Pnts')" size=3></div> <div id="HomePlayer1TotalPnts" style="position:absolute; left:146px; top:37px; width:45px; height:29px; z-index:4"> <input name="HP1 Total Points" size=3></div> </form> </div> </body> </html> Is it possible to change the "OK" text in an alert confirm box ?
Hi! I've seen several sites that use what looks like the alert function (I know it isn't), where the site gets darkened and a popup box (similar to that of the alert one) comes up and displays an image (and only an image) ... how is this done? Regards Matthew Hi I have create a website where many features do not support IE versions, Can someone help me on javascript code that can alert the user to use Netscape to access my website.. I mean Javscript alert code that will recommend the user to use netscape to access my website .. Any help will be much appreciated i need the system to think its getting an alert without actualy presenting an alert on the page. i found this but i guess i dont really understand how it works Code: function foo() { doSomething(); fakeAlert("Alert! Alert!", doSomethingAfterTheAlertIsCleared); } i hate to have to do hacks to get something to work but for some reason my simple js will not work without an alert Code: <script type="text/javascript"> <!-- function process_acount() { alert("about to send data"); document.bbautoreg.submit(); } // --> </script> it works perfectly with the alert and the comment if i remove the alert, it wont work, if i remove the <!-- it wont work, if i have the alert without the comment <!-- it wont work. i thought maybe there might be something wrong with the js interpreter i even changed it from html to php using echo for the js and still the same unless i can get this to work i will have no other choice but to use a fake alert why would having an alert make a dif anyway, seems to me thats just crazy, that itself should have no bearing on if it executes or not. Anyone know why that would matter. How would you make a alert box that contains a tabel instead of writting?
I need to alert the answer 31 but keep getting 34. It has to go like this ticket*reward+cost. It would be 24+4+3=31. I would need to times the tickets by rewards then add the cost after. How would I alert this? Code: <html> <head> <link rel="stylesheet" type="text/css" href="movie_ticket.css" /> <title>Movie</title> <script type="text/javascript"> function ticketOrder() { var cost; var ticket; var reward; var movie; movie=document.movieTicket.selUpcomingMovie.value; if(document.movieTicket.radTicket[0].checked) { ticket=document.movieTicket.radTicket[0].value; ticket=parseInt(ticket); } if(document.movieTicket.radTicket[1].checked) { ticket=document.movieTicket.radTicket[1].value; ticket=parseInt(ticket); } if(document.movieTicket.radTicket[2].checked) { ticket=document.movieTicket.radTicket[2].value; ticket=parseInt(ticket); } if(document.movieTicket.radTicket[3].checked) { ticket=document.movieTicket.radTicket[3].value; ticket=parseInt(ticket); } if(document.movieTicket.radTicket[4].checked) { ticket=document.movieTicket.radTicket[4].value; ticket=parseInt(ticket); } if(document.movieTicket.radReward[0].checked) { reward=document.movieTicket.radReward[0].value; reward=parseInt(reward); } else if(document.movieTicket.radReward[1].checked) { reward=document.movieTicket.radReward[1].value; reward=parseInt(reward); } if(document.movieTicket.chkPopcorn.checked) { var popcorn=document.movieTicket.chkPopcorn.value; popcorn=parseInt(popcorn); cost=popcorn; } if(document.movieTicket.chkSoda.checked) { var soda=document.movieTicket.chkSoda.value; soda=parseInt(soda); cost=soda; } if(document.movieTicket.chkCandy.checked) { var candy=document.movieTicket.chkCandy.value; candy=parseInt(candy); cost=candy; } alert(ticket*reward+"cost"); } </script> </head> <body> <div id="Header"> <h2>The Chicago Movie Palace</h2> <p><h2>We have the best ticket prices!!</h2></p> </div> <div id="main_content"> <form name="movieTicket"> How many tickets would you like to order?<br /> 1 Ticket<input type="radio" name="radTicket" value="1" /><br /> 2 Tickets<input type="radio" name="radTicket" value="2" /><br /> 3 Tickets<input type="radio" name="radTicket" value="3" /><br /> 4 Tickets<input type="radio" name="radTicket" value="4" /><br /> 5 Tickets<input type="radio" name="radTicket" value="5" /><br /> Are you a member of our theater?<br /> Yes<input type="radio" name="radReward" value="8" /><br /> No<input type="radio" name="radReward" value="10" /><br /> Would you like any food or drinks with your movie?<br /> Popcorn ($4)<input type="checkbox" name="chkPopcorn" value="4" /><br /> Soda ($3)<input type="checkbox" name="chkSoda" value="3" /><br /> Candy ($3)<input type="checkbox" name="chkCandy" value="3" /><br /> Which movie would you like to see? <select name="selUpcomingMovie"> <option value="Immortals">Immortals</option> <option value="J Edgar">J Edgar</option> <option value="Sherlock Holmes:A Game of Shadows" >Sherlock Holmes:A Game of Shadows</option> </select> <p><input type="button" name="btnSubmit" value="Order Tickets" onclick="ticketOrder()" /></p> </div></form> <div id="location_info"> <p><span class="highlight">4789 N. Potterville St.</span></p> <p><span class="highlight">Telephone Number 1-312-589-6217</span></p> </div> </body> </html> When you do an alert confirm you can make if statements to say if they click ok do this and if they click cancel stop the program. Well I am trying to make a prompt that first asks the user to enter in a url and after they do and click enter they url is wraped with img tags. I would like to make it so that if the user clicks cancel the program ends b/c right now if the user click okay with no value the result is [img][/img] and if the user clicks cancel the result is [img]null[/img]. Any step in the right direction would be greatly appreciated! PHP Code: <script language="javascript"> function imgGenerator(){ var question=confirm("Would you like to share an image?"); if (question==true){ var obj=document.getElementById("mngl-board-post-input"); var imgurl = prompt("To Share an image paste the image url here"); var txt=document.createTextNode("[img]"+imgurl+"[/img]"); obj.appendChild(txt); } else {} } </script> May I put some style to an alert box ? for example . <html> <head> <style> #box { font-style: strong } </style> <script lenguaje="javascript"> var age; age = prompt("Type age : ", ""); if (edad<18) { <div id=box alert("Underage, get out !")> </div> } else { alert("You are Welcome, surf it it"); } </script> </head> </html> Thanks <html> <head> Filename: oae.htm Supporting files: figa.jpg, figb.jpg, figc.jpg, figd.jpg, figures.jpg, functions.js, oae.jpg, quiz.css --> <title>Online Aptitude Quiz: Page</title> <link href="quiz.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="functions.js"></script> <script type="text/javascript"> var seconds = 0; var clockId; var quizclock; function runClock() { seconds++; quizclock = seconds; document.quiz.quizclock.value = seconds; } function startClock() {showQuiz(); clockId = setInterval("runClock()",1000); } var timer; function stopClock() { clearInterval(runClock); var correctAns = gradeQuiz(); alert("You have Code: correctAns correct of 5 in Code: timer seconds."); } </script> </head> <body> <form id="quiz" name="quiz" action=""> <div id="header"> <p><img src="oae.jpg" alt="Online Aptitude Exam" /> <span>Grunwald Testing, Inc.</span><br /> 1101 Science Drive<br /> Oakdale, CA 88191 </p> </div> <div id="intro"> <p>This is a timed quiz of intelligence and perception. Your final score will be based on the number of correct answers and the time required to submit those answers.</p> <p>To start the quiz click the <b>Start Quiz</b> button below, which will reveal the first page of quiz questions and start the timer. When you have completed the questions, click the <b>Submit Answers</b> button on the quiz form.</p> <p id="buttons"> <input type="button" value="Start Quiz" onClick="startClock()" /> <br /> <input name="quizclock" id="quizclock" value="0" /> </p> </div> <div id="questions"> <h1>Page 1: Pattern Recognition</h1> <table id="quiztable"> <tr> <th rowspan="3">1.</th> <td colspan="2">Enter the next number in this sequence: 1, 3, 4, 7, 11, 18, ...</td> </tr> <tr> <td><input type="radio" name="q1" />a) 22</td> <td><input type="radio" name="q1" />c) 28</td> </tr> <tr> <td id="cor1"><input type="radio" name="q1" />b) 29</td> <td><input type="radio" name="q1" />d) 32</td> </tr> <tr> <th rowspan="3">2.</th> <td colspan="2">Enter the final three numbers in this sequence: 8, 5, 4, 9, 1, 7, 6, ...</td> </tr> <tr> <td id="cor2"><input type="radio" name="q2" />a) 10, 3, 2</td> <td><input type="radio" name="q2" />c) 2, 3, 10</td> </tr> <tr> <td><input type="radio" name="q2" />b) 2, 10, 3</td> <td><input type="radio" name="q2" />d) 10, 2, 3</td> </tr> <tr> <th rowspan="3">3.</th> <td colspan="2">Enter the next letter in this sequence: j, f, m, a, m, j, j, ...</td> </tr> <tr> <td><input type="radio" name="q3" />a) j</td> <td><input type="radio" name="q3" />c) f</td> </tr> <tr> <td><input type="radio" name="q3" />b) m</td> <td id="cor3"><input type="radio" name="q3" />d) a</td> </tr> <tr> <th rowspan="3">4.</th> <td colspan="2">What letter in this set does not belong?: A, B, D, G, J, S, O</td> </tr> <tr> <td id="cor4"><input type="radio" name="q4" />a) A</td> <td><input type="radio" name="q4" />c) J</td> </tr> <tr> <td><input type="radio" name="q4" />b) B</td> <td><input type="radio" name="q4" />d) O</td> </tr> <tr> <th rowspan="3">5.</th> <td colspan="2">What is the next figure in the following sequence?:<br /> <img src="figures.jpg" alt="" /> </td> </tr> <tr> <td><input type="radio" name="q5" />a) <img src="figa.jpg" alt="" /></td> <td><input type="radio" name="q5" />c) <img src="figc.jpg" alt="" /></td> </tr> <tr> <td><input type="radio" name="q5" />b) <img src="figb.jpg" alt="" /></td> <td id="cor5"><input type="radio" name="q5" />d) <img src="figd.jpg" alt="" /></td> </tr> <tr> <td colspan="3" style="text-align: center"> <hr /> <input type="button" value="Submit Answers" onClick="stopClock()"/> </td> </tr> </table> </div> </form> </body> </html> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Color Changer</title> <link href="style.css" rel="stylesheet" type="text/css"> <script type="text/javascript"> window.alert("The page is loading."); </script> </head> <body> <form> <p>Click the button to turn the page: <input type="button" value="Red" onclick="document.bgColor='red';" window.alert("Background is now red.");> </p> <p>Double click the button to turn the page: <input type="button" value="Green" ondblclick="document.bgColor='green';"></p> <p>Click down on the button to turn the page: <input type="button" value="Orange" onmousedown="document.bgColor='orange';"></p> <p>Release the mouse while on the button to turn the page: <input type="button" value="Blue" onmouseup="document.bgColor='blue';"></p> <p> Black button: <input type="button" value="Black" onmousedown="document.bgColor='black';" onmouseup="document.bgColor='white';"></p> </form> <hr> </body> </html> This is all php except for the java code I am struggling with. Please advise. I have tried this 2 ways and have spent hours on something that should be simple. I have a string that I am trying to display in an alert box with an onclick method 1: $mystring = "<a href=\"mysite.com\">Click</a>"; this does not work echo "<input type='button' value='Grab Code' onClick=alert('$mystring')>"; nor does this echo "<input type='button' value='Grab Code' onClick=alert('".$mystring."')>"; nor does anything I try. All I want to do is display the value of mystring in an alert box. However, this does work .. echo "<script type='text/javascript'> alert('".$mystring."');</script>"; But I cannot call this from an onclick. Any help is appreciated. Thanks JT The alert at the top of this function will not show... Code: function getXmlHttpRequestObject() { alert("HTTP XML REQUEST!"); var httpxml; if (window.XMLHttpRequest) { // code decent browsers like Firefox, Chrome, Opera, Safari. And IE7+... httpxml = new XMLHttpRequest(); return httpxml; } else { if (window.ActiveXObject) { //Code for crap like IE httpxml = new ActiveXObject('Microsoft.XMLHTTP'); return httpxml; } else { httpxml = null; } } if (httpxml == null) { document.getElementById('p_status').innerHTML = "Status: Could not create XmlHttpRequest Object, please upgrade your browser!"; return null; } else { return httpxml; } |