Results 1 to 10 of 10
  1. #1

    Post Help is needed with very simple java script

    Hello


    This is a quick question only if you know it

    I want this javascript, that choose from several values, and print to webpage only one of them:


    <script>
    var datan=5
    var ndata=new Array(datan)
    ndata[1]="data_no_1"
    ndata[2]="data_no_2"
    ndata[3]="data_no_3"
    ndata[4]="data_no_4"
    ndata[5]="data_no_5"
    var MachoManRandomSavage=Math.floor(Math.random()*datan)
    var resprint=ndata[MachoManRandomSavage]

    document.write(resprint)
    </script>




    I simply want it to print the output (value of variable "resprint") into this box (instead of the word "data_no_1"):

    <input type="text" size="80" value="data_no_1">


    And any help is highly appreciated.

    Thanks
    Signature

  2. #2
    Is it any hard? .. I don't think at all, but I tried few ways, but only seem to have an error (the box disappears).
    Signature

  3. #3
    Join Date
    Jan 2010
    Location
    MT
    Posts
    45
    Try changing
    Code:
    document.write(resprint)
    to:
    Code:
    document.getElementById('mytextfield').value = resprint
    Then, add 'id="mytextfield"' to the <input> tag:
    Code:
    <input type="text" size="80" value="data_no_1" id="mytextfield">
    Then you are set. document.write() only writes the variable contents, but document.getElementById().value changes the value on the text box with the ID listed.
    Shout! Radio Services
    Developer of Shout! Radio Automation
    Complete cloud-based radio station automation.

  4. #4
    Thank you very much.

    But what should I put as value?

    because the value like this:

    value="data_no_1"

    means in the text box what is printed is data_no_1

    and by removing the value all together, I get an empty box

    Thanks anyway for help
    Signature

  5. #5
    Join Date
    Apr 2008
    Posts
    306
    PHP Code:
    <input id='myTextField' type='text' size='80' value='data_no_1' />

    <
    script type='text/javascript'>
    var 
    ndata = new Array('data_no_1',
                          
    'data_no_2',
                          
    'data_no_3',
                          
    'data_no_4',
                          
    'data_no_5');
                          
    var 
    MachoManRandomSavage Math.floor(Math.random() * ndata.length);
    document.getElementById('myTextField').value ndata[MachoManRandomSavage];
    </script> 
    Heymman - Beefy servers, tiny price !

  6. #6
    Thanks a lot for your reply WootWoot
    That is almost full rewrite of it

    But even though -unfortunately- this will always display in the text box:
    data_no_1


    And if it is not clear: This script is intended to display a random text each time, and it is taken from the five values in the array! The value set in the text box (i.e. data_no_1) is just putted by me to show you that it should display one value from the array each time, but it should not be there actually I think there should be something else, I tried already: value="document.write(resprint)" and didn't work at all.


    Thanks
    Signature

  7. #7
    Join Date
    Apr 2008
    Posts
    306
    This is the proper way to change the value of an input, based on its ID :

    PHP Code:
    document.getElementById('idOfElement').value 'newValue'
    The piece of code I've given works fine. Check out a working example here.
    Refresh the page if you get "data_no_1" as your first random value.

    Most notably, your code was missing input's closing tag ("/>") and semi-columns for the JS.

    Wish you a good continuation!
    Heymman - Beefy servers, tiny price !

  8. #8
    @WootWoot
    Thanks a lot for all.

    That woked only when I put it close to each other, I mean the reson it didn't work firstly was that I put the text box not right above the javascript in the body area, but few other lines far from it, that was why it didn't work, but when I refer to your arrangement again and did exact the same it worked

    Thank you very much!

    I'm wondering if there is a way to include a fixed tag before and after each value in the array, like a <url> before any value printed in the box, and another </url> after each value printed in the box!

    Thank you very much anyway you solved my problem
    Signature

  9. #9
    Join Date
    Apr 2008
    Posts
    306
    Your input tag got to be parsed before the Javascript fires up : otherwise it will throw out an error as what researched element couldn't be found (check out your browser's debugging console). In other words : "input" then "script". It doesn't matter if there's something in between.

    "URL" isn't a known HTML tag. You're referring to the "A" tag and it could be added by browsing all elements of your array, such as :

    PHP Code:
    <input id='myTextField' type='text' size='80' value='data_no_1' />
    <
    ul>
        <
    li>This</li>
        <
    li>works</li>
        <
    li>fine?</li>
    </
    ul>

    <
    script type='text/javascript'>
    var 
    ndata = new Array('data_no_1',
                          
    'data_no_2',
                          
    'data_no_3',
                          
    'data_no_4',
                          
    'data_no_5');

    for (
    i in ndata)
    {
        
    ndata[i] = "<a href='http://www.webhostingtalk.com/'>" ndata[i] + '</a>';
    }
                          
    var 
    MachoManRandomSavage Math.floor(Math.random() * ndata.length);
    document.getElementById('myTextField').value ndata[MachoManRandomSavage];
    </script> 
    Heymman - Beefy servers, tiny price !

  10. #10
    Thank you very much.

    I will contact whenever I need PHP/programming



    btw I PM you about another issue with it. And thanks a lot
    Signature

Similar Threads

  1. Small, simple PHP Script/page - $100 USD
    By AcuNett in forum Software & Scripts Requests
    Replies: 1
    Last Post: 11-30-2007, 08:51 PM
  2. Small script needed
    By RescueHst in forum Employment / Job Offers
    Replies: 2
    Last Post: 12-07-2006, 09:57 PM
  3. Small script needed
    By Mondeo in forum Employment / Job Offers
    Replies: 8
    Last Post: 03-14-2004, 01:43 AM
  4. small simple php script?
    By salsabil in forum Other Offers & Requests
    Replies: 1
    Last Post: 04-28-2003, 11:26 AM
  5. A Small Script Needed
    By ssss in forum Programming Discussion
    Replies: 1
    Last Post: 09-16-2002, 04:40 PM

Posting Permissions

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