/    Sign up×
Community /Pin to ProfileBookmark

Two simple question about submit form with a link

I have two easy questions.

1) I have a form that I want to send while using a link. Here’s what I have but I’m getting an error. Why?

<a href=”javascript:document.getElementById(‘test’).submit();”>send</a>

2) The second thing is that once the user click on the domain I want to add a certain value into a hidden field one the user clicked on the submit link BUT without using a function.

How do I do it?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@balloonbuffoonApr 13.2006 — Here's an example:[code=html]<html>
<head>
<script type="text/javascript">
function subForm(hV) {
document.forms[0].elements["hid_val"].value = hV;
document.forms[0].submit();
return false;
}
</script>
</head>
<body>
<form>
<input type="text" name="blah"><input type="hidden" name="hid_val">
<a href="javascript:void(0);" onclick="subForm('myValue')">submit</a>
</form>
</body>
</html>[/code]


--Steve
Copy linkTweet thisAlerts:
@weeeauthorApr 14.2006 — I tried to have them both in one link but it doesn't work.

<a href="javascript:document.getElementById('test').elements['hid'].value = green; document.getElementById('tst').submit();">send</a>

Why's that?
Copy linkTweet thisAlerts:
@balloonbuffoonApr 14.2006 — First of all, don't refer to a form by ID. Use its name ([I]forms['name'][/I]) or its instance number ([I]forms[0][/I]; 0 if its the first or only form on the page.) Second, you shouldn't really put javascript commands in the "href" tag, use "onclick." Third, if [I]green[/I] is not a variable, it must have single quotes around it.

With those changes in mind, your link should look like this:
[CODE]<a href="javascript:void(0);" onclick="document.forms[0].elements['hid'].value = 'green'; document.forms[0].submit();">submit</a>[/CODE]

--Steve
×

Success!

Help @weee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...