I'm having trouble with an array of data created using the 'getElementsByT agName' call. I'm trying to remove duplicates from the Array. code follows: - // creates a list of all keywords and the removes any dublicates and printes the results
-
getKeywords : function() {
-
-
var keywords = articleHolder.getElementsByTagName('span');
-
var savedKeywords = [];
-
for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
-
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
-
}
-
-
if (allKeywords > 0) {
-
var para = document.createElement('p');
-
var strong = document.createElement('strong');
-
para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
-
-
savedKeywords.sort();
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
-
savedKeywords.splice(n,1);
-
-
}
-
else {
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
}
-
}
-
-
-
}
-
-
articleHolder.insertBefore(para, document.getElementById('copyright'));
-
}
13 1999
What kind of errors do you get?
Do you have some code for articleHolder to provide too?
I get no errors, but the code is not removing the duplicates from the Array. Intire code follows. - var pageOptions = {
-
-
articleHolder : null,
-
-
// get the JS to starting doing its job
-
init : function() {
-
if (document.getElementById) {
-
articleHolder = document.getElementById('content'), // the div holding the article;
-
pageOptions.addButtons();
-
pageOptions.getKeywords();
-
}
-
},
-
-
// create the h3, form, p, input, and input text; append the latter three and add function to each of the buttons
-
addButtons : function() {
-
// create title for the section
-
var formTitle = document.createElement('h3');
-
formTitle.appendChild(document.createTextNode('Customize Article Display'));
-
-
var buttonsForm = document.createElement('form');
-
buttonsForm.setAttribute('method','post');
-
buttonsForm.setAttribute('action','');
-
buttonsForm.setAttribute('id','buttons');
-
-
-
-
// variable for each button for asigning value
-
var actionButtons = [];
-
-
// create the p elements with input elements appened to each p element
-
for (var i = 0; i < 4; i++) {
-
var para = document.createElement('p');
-
actionButtons[i] = document.createElement('input');
-
actionButtons[i].setAttribute('type','button');
-
-
buttonsForm.appendChild(para).appendChild(actionButtons[i]);
-
}
-
-
// set deafult value for each button
-
actionButtons[0].setAttribute('value','Increase Line Spacing');
-
actionButtons[1].setAttribute('value','Highlight Keywords');
-
actionButtons[2].setAttribute('value','Highlight Text on Hover');
-
actionButtons[3].setAttribute('value','Hide Quotations');
-
-
// get Element ID for the container and the h3 elements within the container
-
var container = document.getElementById('rightcol');
-
var elementH3 = container.getElementsByTagName('h3');
-
-
-
-
for (var n = 0; n < elementH3.length; n++) {
-
-
// insert the form after the firt non javScript h3 element
-
container.insertBefore(buttonsForm, elementH3[0]);
-
}
-
-
// inserted the h3 title tag at the top of the container
-
container.insertBefore(formTitle, container.firstChild);
-
-
-
// add functions to each button
-
actionButtons[0].onclick = pageOptions.alterLineSpacing;
-
actionButtons[1].onclick = pageOptions.highlightKeywords;
-
actionButtons[2].onclick = pageOptions.highlightSelections;
-
actionButtons[3].onclick = pageOptions.alterQuoteDisplay;
-
-
},
-
-
// modify the line spacing based on the input button value
-
alterLineSpacing : function() {
-
-
if (this.getAttribute('value') == 'Increase Line Spacing') {
-
articleHolder.className = 'morelineheight';
-
this.setAttribute('value','Decrease Line Spacing');
-
}
-
else {
-
articleHolder.className = '';
-
this.setAttribute('value','Increase Line Spacing');
-
}
-
},
-
-
// modify keyword highlight based on the input button value
-
highlightKeywords : function() {
-
-
var theKeywords = articleHolder.getElementsByTagName('span');
-
var allKeywords = theKeywords.length;
-
if (this.getAttribute('value') == 'Highlight Keywords') {
-
for (var i=0; i<allKeywords; i++) {
-
theKeywords[i].className = 'highlightword';
-
}
-
this.setAttribute('value','No Keyword Highlights');
-
}
-
else {
-
for (var i=0; i<allKeywords; i++) {
-
theKeywords[i].className = '';
-
}
-
this.setAttribute('value','Highlight Keywords');
-
}
-
},
-
-
// modify text highlights based on the input button value
-
highlightSelections : function() {
-
-
var paragraphs = articleHolder.getElementsByTagName('p');
-
var allParas = paragraphs.length;
-
if (this.getAttribute('value') == 'Highlight Text on Hover') {
-
for (var i=0; i<allParas; i++) {
-
paragraphs[i].onmouseover = function() {
-
this.className = 'highlightpara';
-
}
-
paragraphs[i].onmouseout = function() {
-
this.className = '';
-
}
-
}
-
this.setAttribute('value','No Text Highlights');
-
}
-
else {
-
for (var i=0; i<allParas; i++) {
-
paragraphs[i].onmouseover = null;
-
paragraphs[i].onmouseout = null;
-
}
-
this.setAttribute('value','Highlight Text on Hover');
-
}
-
-
},
-
-
// modify the display of quotations based on the input button value
-
alterQuoteDisplay : function() {
-
-
var blockquotes = articleHolder.getElementsByTagName('blockquote');
-
for (var i=0, allBlockquotes = blockquotes.length; i<allBlockquotes; i++) {
-
if (this.getAttribute('value') == 'Hide Quotations') {
-
blockquotes[i].className = 'hide';
-
this.setAttribute('value','Show Quotations');
-
}
-
else {
-
blockquotes[i].className = '';
-
this.setAttribute('value','Hide Quotations');
-
}
-
}
-
},
-
-
// creates a list of all keywords and the removes any dublicates and printes the results
-
getKeywords : function() {
-
-
var keywords = articleHolder.getElementsByTagName('span');
-
var savedKeywords = [];
-
for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
-
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
-
}
-
-
if (allKeywords > 0) {
-
var para = document.createElement('p');
-
var strong = document.createElement('strong');
-
para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
-
-
savedKeywords.sort();
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
-
savedKeywords.splice(n,1);
-
-
}
-
else {
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
}
-
}
-
-
-
}
-
-
articleHolder.insertBefore(para, document.getElementById('copyright'));
-
},
-
-
addEvent : function(obj, type, func) {
-
if (obj.addEventListener) {obj.addEventListener(type, func, false);}
-
else if (obj.attachEvent) {
-
obj["e" + type + func] = func;
-
obj[type + func] = function() {obj["e" + type + func] (window.event);}
-
obj.attachEvent("on" + type, obj[type + func]);
-
}
-
else {obj["on" + type] = func;}
-
}
-
}
-
-
pageOptions.addEvent(window, 'load', pageOptions.init);
I think that using savedKeywords.s plice(n,1) in the for loop is not appropriate because the array length and then element position are changed after each duplicate deletion.
I'm looking further to find a workaround.
I think that using savedKeywords.s plice(n,1) in the for loop is not appropriate because the array length and then element position are changed after each duplicate deletion.
I'm looking further to find a workaround.
Ok, But should I not get an error. The code loops passed the if condition and does the else condition. I've tried using a temp variable to copy the array and then compare the two, but the condition for the if is never met. Thanks for any help.
savedKeywords[i] = new Array(articleHo lder.getElement sByTagName('spa n')[i].firstChild.nod eValue);
This statement assigns to the element at position i of savedKeywords array a new object of type Array, that is a pointer to a string.
In this case, to compare the strings you can use:
if (savedKeywords[n][0] == savedKeywords[n + 1][0])
OR, remove "new Array" usage, and assign directly the value of the span:
savedKeywords[i] = articleHolder.g etElementsByTag Name('span')[i].firstChild.nod eValue;
Then, in order to remove duplicate elements you could create a new Array that is filled in the "else" section of the "for" loop, after checking if the last added value is different from the current one.
OR, remove "new Array" usage, and assign directly the value of the span:
savedKeywords[i] = articleHolder.g etElementsByTag Name('span')[i].firstChild.nod eValue;
Then, in order to remove duplicate elements you could create a new Array that is filled in the "else" section of the "for" loop, after checking if the last added value is different from the current one.
I've removed new Array from the savedKeyword array, But I'm not clear about about creating the else for the for loop. Could you give me an example?
I mean the "else" section where apeears the line:
para.appendChil d(document.crea teTextNode(save dKeywords[n]));
See my changes bellow: -
savedKeywords.sort();
-
var savedKeywordsNew = [];
-
var lastValue = "";
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
//savedKeywords.splice(n,1);
-
-
}
-
else {
-
If (lastValue != savedKeywords[n]){
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
savedKeywordsNew[savedKeywordsNew.length] = savedKeywords[n];
-
lastValue = savedKeywords[n];
-
}
-
}
-
}
-
-
I mean the "else" section where apeears the line:
para.appendChil d(document.crea teTextNode(save dKeywords[n]));
See my changes bellow: -
savedKeywords.sort();
-
var savedKeywordsNew = [];
-
var lastValue = "";
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
//savedKeywords.splice(n,1);
-
-
}
-
else {
-
If (lastValue != savedKeywords[n]){
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
savedKeywordsNew[savedKeywordsNew.length] = savedKeywords[n];
-
lastValue = savedKeywords[n];
-
}
-
}
-
}
-
-
thanks, that did it.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Alex Hopson |
last post by:
Hi,
I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session variable array
($HTTP_SESSION_VARS = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart =
qty of that product).
This works fine, but I need to be able to have sub options AND colours for
|
by: Andre Ranieri |
last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one...
|
by: DaHool |
last post by:
Hi there !!!
I browsed around the Internet in search for a solution of a little difficult
problem i have in VB.NET....
However, i cannot find a suitable anwser anywhere, so i thought i'll give it
a try here...
Okay, here's the deal:
I am trying to read from unmanaged memory with a class type struct, this
|
by: 4Ankit |
last post by:
hey guys i am having trouble changing my array code to include a 'for/
in' structure
the code i am trying to change is below:
<script type="text/javascript">
var contents = new Array(3)
|
by: _Skare_Krow_ |
last post by:
I have a database with three columns. One is an atomic number, one is a
pic url, and the other is a description. What I'm having trouble doing
is putting the url in one table row and the description in another table
row right below it like this...
<tr<tdsome pic url</td<tdother pic url</td></tr>
<tr><tdsome descripts </td<tdother descript </td></tr>
whilest limiting the rows to about 10 cells each,,....
any ideas?
| |
by: Nathan Sokalski |
last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action
that I want to perform on each of the Array values is:
Private Function AddQuotes(ByVal value As String) As String
Return String.Format("'{0}'", value)
End Function
Basically, I just want to surround each value with single quotes. However, I
am having trouble getting this to work using the System.Array.ForEach
method. This may be partially because I am not very...
|
by: bmerlover |
last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender, System::EventArgs * e) function is called, it is suppose to make the Play button dissapear and the Pause Button Appear as soon as the Play button is clicked. I don't know why it calls the keystrokes function then goes through the while loop...
|
by: jthep |
last post by:
Hi,
I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in main:
char name, address, telno, input;
int yearofbirth, choice, records;
yearofbirth = 0;
choice = -1;
Then here's part of the code where it goes wrong:
|
by: Michellevt |
last post by:
Hi
I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes instead. The following is the java script that i am using and i am having trouble in using a class instead of a style tag because simply replacing the style=\"position:absolute;\" tag with class=\"posiAbs\" where in the external css ".posiAbs...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |