niche Posted December 8, 2011 Share Posted December 8, 2011 Here's what I have. How do I get the new link name into the anchor tag and a way that lets me put them in the dive created by my javascript? <html><head><script type="text/javascript">function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var div = document.createElement('div'); div.setAttribute('id', 'link_container'); div.setAttribute("style","background-color:red;"); div.style.width = '300px'; div.style.height = '100px'; div.style.margin = '-15px 0px 0px 75px'; var txt=xmlhttp.responseText; var end = txt.slice(0,txt.indexOf(",")); //define number of cycles of For Loop var txt2 = txt.substr(txt.indexOf(",")+1); //cut 1st position from string and save remaining info var i = 0; for (i=0;i<=end;i++){ var link = document.createElement('a'); //create anchor var new_link = txt.slice(0,txt.indexOf(",")); //name new link var balance = txt2.substr(txt.indexOf(",")+1); //cut new link from string and store remaning link names } document.getElementsByTagName('body')[0].appendChild(div); document.getElementById('link_container').innerHTML=txt2+end; } } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send();}</script></head><body> <p><b>Start typing a name in the input field below:</b></p> <form> First name: <input type="text" onkeyup="showHint(this.value)" size="20" /> </form> </body></html> Link to comment Share on other sites More sharing options...
niche Posted December 8, 2011 Author Share Posted December 8, 2011 I've developed the script further. How do I get the results from the For Loop into the red div from this point? EDIT: I should've asked whether it appears that I'm thinking ab out this right? <html><head><script type="text/javascript">function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var div = document.createElement('div'); div.setAttribute('id', 'link_container'); div.setAttribute("style","background-color:red;"); div.style.width = '300px'; div.style.height = '100px'; div.style.margin = '-15px 0px 0px 75px'; var txt=xmlhttp.responseText; var strA = txt.slice(0,txt.indexOf(",")); //define number of cycles of For Loop var str = txt.substr(txt.indexOf(",")+1); //cut 1st position from string and save remaining info for (i=0;i<=strA;i++){ var newLink = document.createElement('a'); //create anchor newLink.href = strA; newLink.innerHTML = str[i]; //document.getElementById('linksCont').appendChild(newLink); } document.getElementsByTagName('body')[0].appendChild(div); document.getElementById('link_container').innerHTML=txt;} } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send();}</script></head><body> <p><b>Start typing a name in the input field below:</b></p> <form> First name: <input type="text" onkeyup="showHint(this.value)" size="20" /> </form> </body></html> Link to comment Share on other sites More sharing options...
niche Posted December 8, 2011 Author Share Posted December 8, 2011 I've moved my script even a little further. I'm displaying the last value in the string as a link in the red div, but not the preceding values. I haven't found a way to group/concat the links produced in the For Loop. How can that be done? My code so far: <html><head><script type="text/javascript">function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var div = document.createElement('div'); div.setAttribute('id', 'link_container'); div.setAttribute("style","background-color:red;"); div.style.width = '300px'; div.style.height = '100px'; div.style.margin = '-15px 0px 0px 75px'; //var txt=document.innerHTML=xmlhttp.responseText; var txt=xmlhttp.responseText; var strA = txt.slice(0,txt.indexOf(",")); //define number of cycles of For Loop var str = txt.substr(txt.indexOf(",")+1); //cut 1st position from string and save remaining info for (i=0; i<=strA; i++){ var newLink = document.createElement('a'); //create anchor var linkName = str.slice(0,str.indexOf(",")); newLink.href = linkName+".php"; newLink.innerHTML = linkName; var str = str.substr(str.indexOf(",")+1); } document.getElementsByTagName('body')[0].appendChild(div); //document.getElementById('link_container').innerHTML=str; document.getElementById('link_container').appendChild(newLink);} } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send();}</script></head><body> <p><b>Start typing a name in the input field below:</b></p> <form> First name: <input type="text" onkeyup="showHint(this.value)" size="20" /> </form> </body></html> Link to comment Share on other sites More sharing options...
JamesB Posted December 8, 2011 Share Posted December 8, 2011 you could add the following as the last line of your for loop: div.appendChild(newLink); which would then mean you wouldn't need this line: document.getElementById('link_container').appendChild(newLink); Link to comment Share on other sites More sharing options...
niche Posted December 8, 2011 Author Share Posted December 8, 2011 Thank you very much! I worked on this until 12:30 this morning and was excited to get up this morning to see what the w3c Santa might have given me. I wasn't disappointed. I can think better as a javascripter because of your help. Thanks Again, Niche Link to comment Share on other sites More sharing options...
thescientist Posted December 8, 2011 Share Posted December 8, 2011 was excited to get up this morning to see what the w3c Santa might have given me Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.