Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:59:26+00:00 2026-06-13T19:59:26+00:00

I am using onclick event in anchor tag. when i clcik the anchor, I

  • 0

I am using onclick event in anchor tag. when i clcik the anchor, I am invoking a java script function where I change the value of one input element and submit the form. The value is changed but the form does not submit the value. Please help me on this. FYI that if i create an element and add in the form before submission, it works. Why is not working in the first scenario.

<form name="form" >    
    <input type="hidden" name="input" value="test"/>    
    <a onclick='testMethod("test")'>click </a>    
</form>

script used is

function testMethod(value)
{
    if(serviceName != null && jQuery('#form') != null)
    {
        document.forms['form'].getElementById('input').value = value;
        document.forms['form'].action = jQuery('#newAction').val();
        document.forms['form'].submit();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:59:27+00:00Added an answer on June 13, 2026 at 7:59 pm

    The main problem here is you’re trying to access the input by id when it doesn’t have one set:

    (Added id to form and id to input so we can select them easily):

    <form name="form" id="form">
        <input type="hidden" name="input" id="input" value="test" />
        <a onclick='testMethod("test")' >click</a>
    </form>
    

    And the javascript to match (updating to use jQuery’s selectors since you indicated you have jQuery support available):

    function testMethod(value)
    {
        var form = $('#form');
        if(serviceName != null && form.length)
        {
            $('#input').val(value);
            form.attr('action', $('#newAction').val());
            form.submit();
        }
    }
    

    You can also update your code such that you aren’t including the binding to the onclick method in the DOM, but attaching to it directly in javascript:

    Changing the a element to have an ID:

    <form name="form" id="form">
        <input type="hidden" name="input" id="input" value="test" />
        <a id="submitLink">click</a>
    </form>
    

    The javascript could now look like this:

    $(document).ready(function() {
        $('#submitLink').on('click', function(event) {
            event.preventDefault();
            var form = $('#form');
            if(serviceName != null && form.length)
            {
                $('#input').val(value);
                form.attr('action', $('#newAction').val());
                form.submit();
            } 
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set an HTML field's value using JavaScript when onclick event is
How do I fade in an element after using jquery's .before? jQuery $('.button').on(click, function(event){
I am trying to add an onClick event to an anchor tag ... Previously
I want to change onclick event of an anchor but I can't get it
I'm trying to bind a function to the anchor onclick attribute. I'm not using
I am trying to assign the onclick event to an anchor tag that already
I'm using arbor.js to create a graph. How do I create an onclick event
How can I insert jquery inside an html tag like using it in onclick
I am using elemntFromPoint in order to return onclick some element. I would like
I usually have to bind a JavaScript function to an anchor-click event. That is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.