Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Form Value Via JS

  • 17-07-2007 08:38PM
    #1
    Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭


    I'm trying to change the value of a form element by clicking a link. At the the moment I have a loop that writes a callender and I want people to be able to click on the date and the date appears in a text box. How can I do this. I tried putting a onClick in the link and using a seperate function but neither work. Can someone help?

    Thanks,
    S.


Comments

  • Moderators, Politics Moderators, Paid Member Posts: 44,044 Mod ✭✭✭✭Seth Brundle


    try modifying this...
    <html>
    <head>
    </head>
    <body>
    <form name="form1">
        <input type="text" id="firstname" /><br>
     </form>
     <a href="#" onclick="javascript:document.form1.firstname.value='foo bar';return false">click</a>
     </body>
     </html>
    


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    No, thats what I have at the moment. Heres what I have:
    Javascript:
    document.write('<td><a href="#" id="' + newdate + ' onClick="javascript:document.book.callInput.value='+ newdate +'; return false;">' + newdate + '</a></td>');
    

    HTML:
    <form name="book" action="validate.php" method="post">
    	<input type="text" name="callInput" />
    </form>
    

    Its fairly similar to yours except mine doesn't work :).


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    document.write('<td><a href="#" id="' + newdate + ' onClick="javascript:document.book.callInput.value='+ newdate +'; return false;">' + newdate + '</a></td>');
    

    HTML:
    <form name="book" action="validate.php" method="post">
    	<input type="text" name="callInput" />
    </form>
    

    Your document.write statement is faulty. Check your quotes and double quotes. Pay particular attention to quotes near your newdate variable. Remember that you must close every open quote and enquote every variable.
    Finally, document.book.callInput.value is not DOM code and will only work on certain browsers (which may be what is mostly causing your problem). Replace it with:
    document.getElementById('callInput').value
    

    -RD


  • Registered Users, Registered Users 2 Posts: 3,890 ✭✭✭cgarvey


    document.getElementById('callInput').value
    

    Close, but that won't work unless he has no "id" attribute in his "input" tag. So add one, or use the old reliable full DOM path ...
    document.forms['book'].elements['callInput'].value = 'xyz';
    
    . Watch out for everything else Ron mentions


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    cgarvey wrote:
    Close, but that won't work unless he has no "id" attribute in his "input" tag. So add one, or use the old reliable full DOM path ...
    document.forms['book'].elements['callInput'].value = 'xyz';
    
    . Watch out for everything else Ron mentions
    document.getElementByName('callInput').value
    

    If no id is set. Sorry about that, just scanned it, saw callInput and assumed it was an id. document.forms causes me no end of headaches cross browser wise any time I use it that's why I use .getElementById/.getElementByName instead, straight to the tag you're looking to use no hassle.

    -RD


  • Advertisement
Advertisement