By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

repeated element - why?!

P: n/a
Hello,

For some reason, which I cannot see, the code below repeats the last
element of the array at the beginning of the data sent via email. Can
onyone see why?

Thanks

Geoff
{
for (var i = 0; i < slider_value.length; i++)
{
document.getElementById("Slider1ValueText").innerH TML +=
this.slider_value[i] + " ";
}

var situation = "Social Group";
var url = 'http://website/path/cgi-bin/formmail-nms2.cgi';

var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}
Sep 4 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Geoff Cox said the following on 9/4/2005 3:36 AM:
Hello,

For some reason, which I cannot see, the code below repeats the last
element of the array at the beginning of the data sent via email. Can
onyone see why?


Perhaps because the last element of the array happens to be the original
value of the innerHTML? Then, when you append each array element, you
are starting with the last element of the array first, which duplicates it.

Beyond that, without seeing actual code from the page, including the
HTML, it is impossible to tell.

It may well also depend on how your AJAX function is setup.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 4 '05 #2

P: n/a
On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
<Hi************@aol.com> wrote:
Beyond that, without seeing actual code from the page, including the
HTML, it is impossible to tell.

It may well also depend on how your AJAX function is setup.


Randy,

Wonder if this helps? The first code works OK, the second is the one I
posted before which has the repeat of the last value - can you see
what is wrog with the second code?

Cheers

Geoff

------------OK code-----------------

function saveIt()
{

var str_questions = "";
for (var i=0; i < slider_value.length; ++i)
{
str_questions += slider_value[i] + "\n";
}

document.getElementById("Slider1ValueText").innerH TML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});

}

----------problem code-------------

function saveIt()
{

for (var i = 0; i < slider_value.length; i++)
{
document.getElementById("Slider1ValueText").innerH TML
+= this.slider_value[i] + " ";
}

var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}
Sep 4 '05 #3

P: n/a
Geoff Cox said the following on 9/4/2005 12:39 PM:
On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
<Hi************@aol.com> wrote:

Beyond that, without seeing actual code from the page, including the
HTML, it is impossible to tell.

It may well also depend on how your AJAX function is setup.

Randy,

Wonder if this helps? The first code works OK, the second is the one I
posted before which has the repeat of the last value - can you see
what is wrog with the second code?


It helps and points to my original answer. The first approach is
actually a more efficient and preferred way of coding it. The first one
only does one lookup of the innerHTML property, the second does repeated
lookups.

The second difference in the two codes is how you go about adding the
new values to the innerHTML. In the first code, you basically say "OK,
take these values, build a string called str_questions, and then put
that value in the innerHTML of a div tag replacing whatever is in the
div tag". In the second one, you are saying "OK, take these values and
add it to whatever is already in the DIV tag".

And that is where your problem lies (as indicated in my first post). Cheers

Geoff

------------OK code-----------------

function saveIt()
{

var str_questions = "";
for (var i=0; i < slider_value.length; ++i)
{
str_questions += slider_value[i] + "\n";
}

document.getElementById("Slider1ValueText").innerH TML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});

}

----------problem code-------------

function saveIt()
{

Add an alert right here:

alert(document.getElementById("SliderValueText").i nnerHTML)

And I bet you will see the last value of your array entries.
for (var i = 0; i < slider_value.length; i++)
{
document.getElementById("Slider1ValueText").innerH TML
+= this.slider_value[i] + " ";
}

var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 4 '05 #4

P: n/a
Lee
Geoff Cox said:
Wonder if this helps? The first code works OK, the second is the one I
posted before which has the repeat of the last value - can you see
what is wrog with the second code?


In your first code, you start with an empty string (str_questions)
and append each slider_value to it, then set the innerHTML to the
value of str_questions. So innerHTML contains nothing except the
values from slider_value.

In the second code, you start with an innerHTML value which is NOT
empty, and append each slider_value to it. Naturally it will
contain something at the beginning that you don't really want.

Sep 4 '05 #5

P: n/a
On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
<Hi************@aol.com> wrote:
Wonder if this helps? The first code works OK, the second is the one I
posted before which has the repeat of the last value - can you see
what is wrog with the second code?


It helps and points to my original answer. The first approach is
actually a more efficient and preferred way of coding it. The first one
only does one lookup of the innerHTML property, the second does repeated
lookups.

The second difference in the two codes is how you go about adding the
new values to the innerHTML. In the first code, you basically say "OK,
take these values, build a string called str_questions, and then put
that value in the innerHTML of a div tag replacing whatever is in the
div tag". In the second one, you are saying "OK, take these values and
add it to whatever is already in the DIV tag".

And that is where your problem lies (as indicated in my first post).


Thanks Randy for the explanation - will stick with the first code!

Cheers

Geoff


Cheers

Geoff

------------OK code-----------------

function saveIt()
{

var str_questions = "";
for (var i=0; i < slider_value.length; ++i)
{
str_questions += slider_value[i] + "\n";
}

document.getElementById("Slider1ValueText").innerH TML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});

}

----------problem code-------------

function saveIt()
{


Add an alert right here:

alert(document.getElementById("SliderValueText"). innerHTML)

And I bet you will see the last value of your array entries.
for (var i = 0; i < slider_value.length; i++)
{
document.getElementById("Slider1ValueText").innerH TML
+= this.slider_value[i] + " ";
}

var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}


Sep 4 '05 #6

P: n/a
On 4 Sep 2005 10:27:17 -0700, Lee <RE**************@cox.net> wrote:
Geoff Cox said:
Wonder if this helps? The first code works OK, the second is the one I
posted before which has the repeat of the last value - can you see
what is wrog with the second code?


In your first code, you start with an empty string (str_questions)
and append each slider_value to it, then set the innerHTML to the
value of str_questions. So innerHTML contains nothing except the
values from slider_value.

In the second code, you start with an innerHTML value which is NOT
empty, and append each slider_value to it. Naturally it will
contain something at the beginning that you don't really want.


Lee,

Many thanks - I see what you mean.

Cheers

Geoff

Sep 4 '05 #7

P: n/a
On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
<Hi************@aol.com> wrote:
The second difference in the two codes is how you go about adding the
new values to the innerHTML. In the first code, you basically say "OK,
take these values, build a string called str_questions, and then put
that value in the innerHTML of a div tag replacing whatever is in the
div tag". In the second one, you are saying "OK, take these values and
add it to whatever is already in the DIV tag".
Randy,

Although I have gone back to the other code so the app works OK just
wonder about the above.

I am creating the <SPAN etc as part of building a table

document.write("<SPAN class='invisible'
ID='Slider1ValueText'></SPAN>");

so don't see how the last slider value, which appears repeated as the
first value sent by email, can come from here?!

Geoff



And that is where your problem lies (as indicated in my first post).
Cheers

Geoff

------------OK code-----------------

function saveIt()
{

var str_questions = "";
for (var i=0; i < slider_value.length; ++i)
{
str_questions += slider_value[i] + "\n";
}

document.getElementById("Slider1ValueText").innerH TML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});

}

----------problem code-------------

function saveIt()
{


Add an alert right here:

alert(document.getElementById("SliderValueText"). innerHTML)

And I bet you will see the last value of your array entries.
for (var i = 0; i < slider_value.length; i++)
{
document.getElementById("Slider1ValueText").innerH TML
+= this.slider_value[i] + " ";
}

var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1V alueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}


Sep 5 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.