Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Form Value Via JS

  • 17-07-2007 7: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 Posts: 41,211 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,889 ✭✭✭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