2

I have an html form on my page that I want to send to a javascript function. How do I send values to that function/activate it so the function runs?

<form  method="post" action="WHAT GOES HERE?">
<input type = "textarea" name = "link" placeholder="Submit a Link" style="color:black;max-width:133px;"></input>
<input type = "hidden" name = "ref" value="yes"></input>
<a href="javascript:void(0)" onclick="submit();">Submit</a> 

3 Answers 3

4

You don't "send" the form anywhere. Javascript can read the form values at any time. You usually attach an onsubmit event to the form or an onclick event to the submit button, then when that happens you use Javascript to read in the form values.

For example, you can read the value of your "ref" input by first giving it an id...

<input id="ref" name="ref" ...

Then read it with Javascript like so,

document.getElementById('ref').value

It's up to you what you do with that value.

Also, your HTML is a bit off. Inputs should be self closing (<input/>) and to create a textarea you're supposed to use <textarea></textarea>.

Edit: If you just wanted a 1-line textbox, it should be <input type="text" ... not textarea. The reason it works is because the default for an input happens to be text.

Sign up to request clarification or add additional context in comments.

1 Comment

DING DING DING! Awesome! Thank you!
0

you can use the forms onsubmit event

<form  method="post" action="" onsubmit="validateForm()">
<input type = "textarea" name = "link" placeholder="Submit a Link" style="color:black;max-width:133px;"></input>
<input type = "hidden" name = "ref" value="yes"></input>
<a href="javascript:void(0)" onclick="submit();">Submit</a> 

5 Comments

Is validateForm a builtin function?
also, how do you then retrieve the values for link and ref?
you'll have to set up your own javascript function in this case "validateForm". You can then select the inputs and validate as needed
oh so validateForm is my javascript function name, within that function, how do I access the link value? and the "ref" value?
you can use a jQuery selector on the input's name. something like this here: api.jquery.com/attribute-contains-selector
0
<html>
<head>


<script type="text/javascript">
function getValue(){
var textValue=document.getElementById("textId").value;
alert(textValue);
}
</script>


</head>
<body>
<form  method="post" >
<input type = "textarea" id="textId" name = "link" placeholder="Submit a Link"       style="color:black;max-width:133px;"></input>
<input type = "hidden" name = "ref" value="yes"></input>
<a href="#" onclick="getValue();">Submit</a> 
</form>
</body>

</html>

1.create id for textarea. id="textId"

2.Fetch the value of textarea using the id created in the javascript using the method document.getElementById("textId").value;

1 Comment

You don't need javascript: in onXXX attributes; that's only needed in href.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.