JavaScript - Adding Html Links To An Array
Similar TutorialsI've got this little chunk of code running on my freelance portfolio site - on each page (different sections of my portfolio) I have 6 thumbnails and a full sized graphic with some descriptive info, you click the thumbnail and it switches the graphic and other info, that all works fine. But on one page I have some web stuff and I'd like to make the var 'clientinfo' into a link to go to the client's website. I know I need to add an array with the website links, but then I'm not sure how to implement it into the html, I've tried various things I've found on here but I'm not getting anywhere on my own. I know it's something pretty simple (for someone who knows what they're doing) but I just can't seem to grasp it. Can anyone help? the js is like this: $(function() { $(".image").click(function() { var image = $(this).attr("rel"); var title = $(this).attr("title"); var description = $(this).attr("content"); var clientinfo = $(this).attr("clientinfo"); $('#gallery').hide(); $('#gallery').fadeIn('slow'); $('#image').html('<img src="' + image + '"/>'); $('#title').html(title); $('#description').html(description); $('#client').html(clientinfo); return false; }); and then the HTML anchors are like this: <a href="#" rel="images/gallery/web/site1.png" title="Site One" content="This is the descriptive info for site number one" clientinfo="Site One link" class="image"><img src="images/gallery/web/t_site1thumb.png" class="thumb" border="0"/></a> Years ago I created HTML that employs checkboxes and textboxes. I am now writing JS with the intention of adding flexibility and limiting redundancy. I am not sure I truly understand how to correctly interact the two though. For example, one of my scripts have arrays that contain the names of the checkboxes and textboxes, with a 'for' loop to document.write() them to references within the HTML code. This does not seem to be working for me though. Here is what I have thus far (in short): Code: <script language="javascript"> var teamNames = new Array(3); teamNames[0]="South Africa"; teamNames[1]="Mexico"; teamNames[2]="Uruguay"; for (i=0; i<=31; i++) { document.write("<p>" + teamNames[i] + "<\/p>"); } </script> </head> <body> <tr><td>Jun 11</td><td><input type="checkbox" name="teamNames[0]" value="teamAbbr[0]"></td> </body> I've left out a lot of the code (to include the teamAbbr array, but you get the points. I've tried moving the JS within the HTML body and playing with the reference syntax, but nothing so far. Any help would be great! On my website, I have random logos appear on the home page as products that I recommend. I would like to make these logos into links to the website they are from, so that when one clicks on the picture, it directs you to the site, but I can't quite get it to work, and I know very little about Javascript (as you may know from my previous question about Javascript). If someone could help me crack the code, that would be great Here's my current JS code: Code: <!-- // Set up the image files to be used. var theImages = new Array() // do not change this // To add more image files, continue with the // pattern below, adding to the array. Rememeber // to increment the theImages[x] index! theImages[0] = 'http://i200.photobucket.com/albums/aa216/Jilldear/classicequinelogo-1.jpg' theImages[1] = 'images/logo2.jpg' theImages[2] = 'http://i200.photobucket.com/albums/aa216/Jilldear/pyranha-1.jpg' // ====================================== // do not change anything below this line // ====================================== var j = 0 var p = theImages.length; var preBuffer = new Array() for (i = 0; i < p; i++){ preBuffer[i] = new Image() preBuffer[i].src = theImages[i] } var whichImage = Math.round(Math.random()*(p-1)); function showImage(){ document.write('<img src="'+theImages[whichImage]+'">'); } //--> And my current HTML code: Code: <script language="JavaScript"> <!-- showImage(); //--> </script> Sorry for being so green with JS I want to make a script that will insert all text type inputs into an array. From there I want to be able to call them and edit them. Here is what I have so far and it is not working. var phone1 = '702' var inputArray = new Array(); var inputs = document.getElementsByTagName('input'); inputs; if (input.type == 'text') { inputArray.push(inputs.id); } inputArray.reverse(); inputArray[0].value = phone1; I know you can add data to array wiht the push() method but can you add a whole new line as in a = new Array a [1] [1,2,3] so it would create a new line a [2] [4,5,6]; The syntax to the array i think is wrong but you get the question. Okay so im just doing a little test and this might seem like a really stupid question but i cant figure it out...all i simply want to do is take all those numbers and get the sum then display the sum in a document.write in theory i should get 10 but i dont and idk why i have tried many many things Code: var numbers = new Array(); numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; numbers[3] = 4; thanks Resolution: Code: <script type="text/javascript"> function amountTotal() { var totalDonations = 0; for (var i = 0; i < amount.length; i++) { totalDonations += amount[i]; } document.write(totalDonations); } </script> I have an array in a seperate.js file that I need to step through a FOR loop to grab all these values and add them together. Here is a small sample of the array's: Code: city = new Array(); state= new Array(); zip = new Array(); amount = new Array(); date = new Array() firstName[0]="Nina"; lastName[0]="Largent"; street[0]="88 Regal Lane"; city[0]="Williamsburg"; state[0]="KY"; zip[0]="40769"; amount[0]=125; date[0]="2011-09-18"; firstName[1]="Mike"; lastName[1]="Hart"; street[1]="Da404 Barrow Street"; city[1]="London"; state[1]="KY"; zip[1]="40744"; amount[1]=75; date[1]="2011-09-18"; Here is the loop step through that I have: Code: <script src="arrays.js" type="text/javascript"></script> <script type ="text/javascript"> function amountTotal() var totalDonations = 0 for (var i=0; i < amount.length; i++) { } </script> I am stuck on how to grab each amount index and add them together to save them as the amountTotal variable. Hi, I have an external JS file with an array of menu links called (menu.js). The code is shown below: Code: <script> var menuItem = new Array(); menuItem[0]="index.html"; menuItem[1]="welcome_home/index.html"; menuItem[2]="key_resources/index.html"; menuItem[3]="deployment_assistance/index.html"; menuItem[4]="benefits/index.html"; menuItem[5]="services/index.html"; menuItem[6]="counties/index.html"; menuItem[7]="transitional_help/index.html"; menuItem[8]="women_vets/index.html"; menuItem[9]="iowa_heroes/index.html"; menuItem[10]="http://www.iowava.org/vetcemetery/"; menuItem[11]="http://ivh.iowa.gov"; menuItem[12]="../forms/index.html"; menuItem[13]="https://va.iowa.gov/reselig/"; menuItem[14]="links2/index.html"; menuItem[15]="Whats_New/index.html"; menuItem[16]="contact_us/index.html"; function Page(which){ location.href = menuItem[which]; } </script> I have called the javascript in my websites main page (index.html), but it does not work. The html code is shown below: Code: <script type ="text/javascript" src="menu.js"></script> <a href="javascript:void(0);" onclick="Page(0);">Home</a> <a href="javascript:void(0);" onclick="Page(1);">Welcome Home</a> <a href="javascript:void(0);" onclick="Page(2);">Key Resources</a> The javascript for the array of menu links works when it is inserted directly within the html file, but when it remains external and is called in index.html it does not work. I am not sure why this is the case. Could anyone give some pointers? Thanks, -Mike Hi all, I have a div element (i.e. <div id="main">) that contains a number of links, each with white background colors and its top and left style properties set to different values. I need to do several things with the anchors when they are clicked: When a white anchor is clicked, its background color is turned yellow and its coordinates (top and left style property values) are added to an array. The coordinates for a single anchor is to be separated by a hyphen; coordinates for multiple anchors are to be separated by a comma (eg. "0-9,0-18") When a yellow anchor is clicked, its background color is turned white and its coordinates are removed from the array. The values in the array are to be the value for a hidden input element, all separated by commas. I'm a Javascript novice and don't know where to begin. Can someone help me with this? Thanks in advance, Dude-Dastic Hi, I am relatively new to JavaScript and I am trying to write a script to switch style sheets when a user clicks a button. I got my script to work. I created buttons with in the script, put them in a div, and used appendChild to add the div to the page. This all works and the style sheets switch, however I want to add a cookie, so when the user returns to the page it displays with the style sheet they were using last. I can't figure out how to grab which style sheet the user selected, to add it to the cookie. I have included the code for the switching. Code: function addEvent(object, evName, fnName, cap) { if (object.attachEvent) object.attachEvent("on" + evName, fnName); else if (object.addEventListener) object.addEventListener(evName, fnName, cap); } addEvent(window, "load", makeStyleButton, false); var allStyles = new Array(); function makeStyleButton() { var allLinks = document.getElementsByTagName("link"); for(var i = 0; i<allLinks.length; i++) { if((allLinks[i].rel == "stylesheet" || allLinks[i].rel == "alternate stylesheet") && allLinks[i].rel.title !="") { allStyles.push(allLinks[i]); } } var styleBox = document.createElement("div"); for(var i =0; i<allStyles.length; i++) { if(allStyles[i].rel == "stylesheet") { allStyles[i].disabled = false; } else { allStyles[i].disabled = true; } styleButton = document.createElement("input"); styleButton.type = "button"; styleButton.value = allStyles[i].title + " view"; styleButton.title = allStyles[i].title; styleButton.style.width = "125px"; styleButton.style.fontSize = "12px"; styleButton.onclick = changeStyle; styleBox.appendChild(styleButton); } styleBox.style.width = "125px"; styleBox.style.cssFloat = "right"; styleBox.style.styleFloat = "right"; styleBox.style.margin = "5px 5px 10px 10px"; var sourceDoc = document.getElementById("content"); sourceDoc.insertBefore(styleBox, sourceDoc.firstChild); } function changeStyle() { for(var i = 0; i<allStyles.length; i++) { if (allStyles[i].title == this.title) { allStyles[i].disabled = false; } else { allStyles[i].disabled = true; } } } I am not looking for someone to give me the answer, I am just looking for a push in the right direction, so to speak. I seem to be going around in circles with this problem. Thank you for your time. Hi, I have a HTML table in a form with a submit button.In HTML table I have links present in first three rows for each columns of HTML table.I want to disable those links when user submit the form.How can I handle this in javascript? Please help. Thanks in advance. Anil. Trying to add the text "• " to a text area, but it will just print the symbol. Is there a way you can print the string itself? Code: function add(myValue) { //IE support if (document.selection) { desc.focus(); sel = document.selection.createRange(); sel.text = myValue; } Code: ...onclick="add('• ')" Hi everyone, I have successfully added Flash to my page but I want the flash animation to be inside the <div id="main">. For some reason when I put the <div id="flashcontent"></div> inside the main div it doesn't show up on the page. Please help. My code is below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>MikeJamesDesigns</title> <link rel="stylesheet" type="text/css" href="cssEdit.css" /> <script type="text/javascript" src="js/flashobject.js"></script> <script type="text/javascript"> var fo = new FlashObject("bannerISRAEL.swf", "animationName", "450", "60", "8", "#FFFFFF"); fo.addParam("allowScriptAccess", "sameDomain"); fo.addParam("quality", "high"); fo.addParam("scale", "noscale"); fo.addParam("loop", "true"); fo.write("flashcontent"); </script> </head> <body> <!--<div class="tikiImage"> <img src="/Users/MikeJamesRossi/Desktop/cssEdit/images/tiki.png" alt="tikiTorch" height="1040px" width="1100px"/> </div>--> <div id="wrapper"> <div id="sitebranding"> </div><!--end of sitebranding div--> <div id="navigation"> <a href="home">Home</a> | <a href="about">About</a> | <a href="news">News</a> | <a href="images">Images</a> | <a href="home">Faq</a> | <a href="contact">Contact</a> </div><!--end of navigation div--> <div id="main"> <div id="flashcontent"></div> </div><!--end of main div--> </div><!--end of wrapper div--> </body> </html> My css file looks like this: #flashcontent { position: relative; top: 500px; left: 365px; width: 60px; heightx; background-color:red; z-index: ; } Any help would be greatly aprreciated!! Thanks Mike! Hey guys, I have a script which adds more text fields to a div in a form, but when it adds a new field the text in the others is cleared. :S http://martynleeball.goodluckwith.us/ PHP 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>Draw Somthing - Word Guess</title> <script type="text/javascript"> function add_field(obj, count) { //Count how many fields there is so the correct number can be added to the ID var form_name = document.getElementById(obj); var inputs = form_name.getElementsByTagName('input'); var g = 1; //alert("Container: " + obj + ". Inputs: " + inputs.length); for ( var i = 0; i < inputs.length; i++) { var node = inputs[i]; if (node.getAttribute('type') == 'text') { g++; } } //Field to add to the form if (!count) { if (g < 9) { var text_field_html = "<input type=\"text\" id=\"input" + g + "\" name=\"input" + g + "\" /><input type=\"button\" value=\"X\" onClick=\"remove_field(\'text_field_container\', this)\" id=\"close" + g + "\"/>"; form_name.innerHTML += text_field_html + "<br id=\"br_" + g + "\" />"; } } } function remove_field(parent_id, r_obj) { var parent = document.getElementById(parent_id); var child = document.getElementById(r_obj.id); var num = r_obj.id.charAt(r_obj.id.length-1); var child2 = document.getElementById('input' + num); var child3 = document.getElementById("br_" + num); parent.removeChild(child); parent.removeChild(child2); parent.removeChild(child3); } </script> </head> <body> <br /> <form id="add_words" action="test.php" method="post"> <div id="text_field_container"> <input type="text" id="input1" name="input1" /><input type="button" value="X" id="close1" onClick="remove_field('text_field_container', this)"/> <br id="br_1" /> </div> <input type="button" value="Add more..." onclick="add_field('text_field_container')" /> <input type="submit" value="Submit" onclick="" /> </form> </body> </html> Hi there, I am trying to open some SWF videos from an HTML page. When the user clicks the link, I would like to have the videos open in a transparent window (not a separate browser wndow). I have figured out how to embed the files and have them open in a transparent window as soon as you load/refresh the page but I want to open the SWF using a link. Any suggestions? Thank you all. B Hello all! I'm having a bit of an issue here with my lastest project. What I'm trying to do is have a menu that a user would click one of two links which would change the targeted iFrame, then repeat for two more options, and again for two more. It is essentially a filtering system for videos (the best way for you to see what I mean is to check out http://grph.cmwdesign.ca). My actual issue here is the changing of the iframe href, and on top of that, I seem to not to be able to properly get my functions to run all the time. Here is my Javascript: Code: var categoryLink=new Array(); var counter; var link = ""; categoryLink[0] = ""; categoryLink[1] = ""; categoryLink[2] = ""; counter = "0"; $(document).ready(function() { $(".atonal").live('click', function() { { alert("sometext"); if (categoryLink[0]=="") { categoryLink[0] = "atonal"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[0] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); $(document).ready(function() { $(".tonal").live('click', function() { alert("sometext"); if (categoryLink[0]=="") { categoryLink[0] = "tonal"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[0] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); $(document).ready(function() { $(".being").live('click', function() { alert("sometext"); categoryLink[1] = "being"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); if (categoryLink[1]=="") { categoryLink[1] = "being"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[1] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); $(document).ready(function() { $(".doing").live('click', function() { alert("sometext"); if (categoryLink[1]=="") { categoryLink[1] = "doing"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[1] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); $(document).ready(function() { $(".abstract").live('click', function() { alert("sometext"); if (categoryLink[2]=="") { categoryLink[2] = "abstract"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[2] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); $(document).ready(function() { $(".documentary").live('click', function() { alert("sometext"); if (categoryLink[2]=="") { categoryLink[2] = "documentary"; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); else { categoryLink[2] = ""; $("a.frame").attr('href', categoryLink[0] + categoryLink[1] + categoryLink[2]); } }); }); and the HTML in question: Code: <table> <tr> <td height="35" width="210" valign="top">choose your sound</td> <td width="165" valign="top"><a href="#" id="atonal">atonal sounds</a></td> <td width="165" valign="top"><a href="#" id="tonal">tonal sounds</a></td> </tr> <tr> <td height="35" width="210" valign="top">choose your text</td> <td width="165" valign="top"><a href="#" id="being">being words</a></td> <td width="165" valign="top"><a href="#" id="doing">doing words</td> </tr> <tr> <td height="35" width="210" valign="top">choose your image</td> <td width="165" valign="top"><a href="#" id="abstract">abstract images</a></td> <td width="165" valign="top"><a href="#" id="documentary">documentary images</a></td> </tr> </table> I'm no Javascript wiz, so I'm sure I'm probably not going about this entirely the correct way. Any suggestions would be great! Hey guys. This is vague because I dont know what exactly to tell you but please reply with me so I can fix this. I want to add something to my forum. Specifically, I want to add a HTML and Picture button for when you go to reply. Like posting an article here, there are also these options (font, alignment, insert image) I do not know how to achieve this but shouldnt be too hard. Here are the two sources you will need to look through to help me fix the forum. http://neoweather.com/FWFORUM.JS http://static.websimages.com/JS/fw.js THANKS Hey guys. I want to add something to my forum. Specifically, I want to add a HTML and Picture button for when you go to reply. Like posting an article here, there are also these options (font, alignment, insert image) I do not know how to achieve this but shouldnt be too hard. Here are the two sources you will need to look through to help me fix the forum. http://neoweather.com/FWFORUM.JS http://static.websimages.com/JS/fw.js THANKS |