repeated element - why?!

Discussion in 'Javascript' started by Geoff Cox, Sep 4, 2005.

  1. Geoff Cox

    Geoff Cox Guest

    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});
    }
     
    Geoff Cox, Sep 4, 2005
    #1
    1. Advertising

  2. Geoff Cox

    Randy Webb Guest

    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
     
    Randy Webb, Sep 4, 2005
    #2
    1. Advertising

  3. Geoff Cox

    Geoff Cox Guest

    On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
    <> 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 + "\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});
    }
     
    Geoff Cox, Sep 4, 2005
    #3
  4. Geoff Cox

    Lee Guest

    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, Sep 4, 2005
    #4
  5. Geoff Cox

    Randy Webb Guest

    Geoff Cox said the following on 9/4/2005 12:39 PM:
    > On Sun, 04 Sep 2005 03:44:47 -0400, Randy Webb
    > <> 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 + "\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});
    > }



    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
     
    Randy Webb, Sep 4, 2005
    #5
  6. Geoff Cox

    Geoff Cox Guest

    On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
    <> 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 + "\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});
    >> }
     
    Geoff Cox, Sep 4, 2005
    #6
  7. Geoff Cox

    Geoff Cox Guest

    On 4 Sep 2005 10:27:17 -0700, Lee <> 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
     
    Geoff Cox, Sep 4, 2005
    #7
  8. Geoff Cox

    Geoff Cox Guest

    On Sun, 04 Sep 2005 13:37:28 -0400, Randy Webb
    <> 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 + "\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});
    >> }
     
    Geoff Cox, Sep 5, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Lipp
    Replies:
    0
    Views:
    379
    Wolfgang Lipp
    Feb 9, 2004
  2. Wolfgang Lipp
    Replies:
    0
    Views:
    362
    Wolfgang Lipp
    Feb 9, 2004
  3. Wolfgang Lipp
    Replies:
    8
    Views:
    416
    Wolfgang Lipp
    Feb 9, 2004
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,033
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page