Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > repeated element - why?!

Reply
Thread Tools

repeated element - why?!

 
 
Geoff Cox
Guest
Posts: n/a
 
      09-04-2005
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});
}
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-04-2005
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
 
Reply With Quote
 
 
 
 
Geoff Cox
Guest
Posts: n/a
 
      09-04-2005
On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
<(E-Mail Removed)> 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});
}
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-04-2005
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.

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      09-04-2005
Geoff Cox said the following on 9/4/2005 12:39 PM:
> On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      09-04-2005
On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
<(E-Mail Removed)> 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});
>> }


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      09-04-2005
On 4 Sep 2005 10:27:17 -0700, Lee <(E-Mail Removed)> 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

 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      09-05-2005
On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
<(E-Mail Removed)> 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});
>> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Repeated loss of Internet access Jeff Wireless Networking 11 03-26-2006 10:44 PM
element farms (containers for repeated elements) needed? Wolfgang Lipp XML 8 02-09-2004 03:24 PM
element farms (containers for repeated elements) needed? Wolfgang Lipp XML 0 02-09-2004 11:00 AM
element farms (containers for repeated elements) needed? Wolfgang Lipp XML 0 02-09-2004 10:58 AM



Advertisments