repeated element - why?!

G

Geoff Cox

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").innerHTML +=
this.slider_value + " ";
}

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

var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' ' +
'Slider_Values='+document.getElementById('Slider1ValueText').innerHTML;
var myAjax = new Ajax.Updater('Status', url, {method: 'post',
parameters: pars});
}
 
R

Randy Webb

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.
 
G

Geoff Cox

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 + "\n";
}

document.getElementById("Slider1ValueText").innerHTML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1ValueText').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").innerHTML
+= this.slider_value + " ";
}

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

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.
 
R

Randy Webb

Geoff Cox said the following on 9/4/2005 12:39 PM:
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 + "\n";
}

document.getElementById("Slider1ValueText").innerHTML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1ValueText').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").innerHTML
+= this.slider_value + " ";
}

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

Geoff Cox

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 + "\n";
}

document.getElementById("Slider1ValueText").innerHTML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1ValueText').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").innerHTML
+= this.slider_value + " ";
}

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

Geoff Cox

Geoff Cox said:


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
 
G

Geoff Cox

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 + "\n";
}

document.getElementById("Slider1ValueText").innerHTML = str_questions;

var situation = "Social Group";
var url = 'http://website/path/formmail-nms2.cgi';
var pars = 'Situation: ' + situation + ' ' + 'Name: ' + name + ' '
+
'Slider_Values='+document.getElementById('Slider1ValueText').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").innerHTML
+= this.slider_value + " ";
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top