SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Show text generated by javascript

    Hello.

    I have a file, baater.js, which I am getting information from.

    I want to make a button, so that when the user clicks it, this code should executed:

    <script type="text/javascript">
    function listmidget500() {
    for (i=0; i<allmidget500struct.length; i++) {
    document.write(allmidget500struct[i] + " : " + allmidget500name[i] + "<br>");
    }
    }
    </script>

    How will the code be? The text that the script fetches should be placed where it stands in the html code.

    Thank you for all answers.

  2. #2
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of this:

    Code:
     document.write(allmidget500struct[i] + " : " + allmidget500name[i] + "<br>");
    you could to this:

    Code:
    document.getElemenyById("output").innerHTML = allmidget500struct[i] + " : " + allmidget500name[i] + "<br>";
    Then, anywhere on the page you could put:

    Code:
    <span id="output"></span>
    So, if you call that function you want with an onClick event on some object, the output would be added within the span tag. You could also use this technique for a textarea, if that is better for your purposes. Is that what you are looking for?

  3. #3
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for answer, but it didn't work as I had hoped. The text generated by the script appeared on a new page, not on the same page where the span tag was... This is the code so far:

    Code:
    Types of Midget:<input type="button" value="&gt;" name="showmidget500" onclick="listmidget500()"><br>
                
                
                <script type="text/javascript">
                function listmidget500() {
            		for (i=0; i<allmidget500struct.length; i++) {
            			document.getElemenyById("output22").innerHTML = allmidget500struct[i] + " : " + allmidget500name[i] + "<br>";
            		}
            	  } 
            </script>
            <span id="output22"></span>
    Hope someone got a solution for this... thanks.

  4. #4
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird. That doesn't make any sense. Where did the new page come from? Are you using the ID in more than one place? Have you tested IE and FF?

    Otherwise, your code seems fine.

  5. #5
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know where the new page comes from. In the address-field it says I am on the same page. I dont use the ID anywhere else, and have tested it in both IE and FF... same thing happens.

  6. #6
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the error... getElementById, not getElemenyById... typo.

    But now a new thing.. The text that should appear in the span section is multiple lines, but only 1 line is shown... Any ideas?

  7. #7
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean that all the data is there on one line or only line of data was displayed?

  8. #8
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only one line of data was displayed

  9. #9
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! You have to concatenate each line onto the other. LIke this:

    Code:
    document.getElementById("output").innerHTML += allmidget500struct[i] + " : " + allmidget500name[i] + "<br>";
    My bad!

  10. #10
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! It worked fine now...

    But any idea how to make the button change from > to < when it has been pressed and then hide the text again?

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •