Jump to content

need *simple* coding help

Joshcanread

I have two input boxes for "first name" and "last name" and i want to use them as variables (inputted text obviously) but they are html based and the rest of my code is in js

Link to comment
Share on other sites

Link to post
Share on other sites

You can reference by id

 

So you'd do something like:

 

<input type="text" id="Textbox">

<script>
{

	var x = document.getElementById("Textbox").value;

}
</script>

 

I haven't used JS in a while, but I'm fairly confident that's how you reference those values.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Slottr said:

You can reference by id

 

So you'd do something like:

 


<input type="text" id="Textbox">

<script>
{

	var x = document.getElementById("Textbox").value;

}
</script>

 

I haven't used JS in a while, but I'm fairly confident that's how you reference those values.

You don't the need the curly braces though.

Link to comment
Share on other sites

Link to post
Share on other sites

To add to what's been said, HTML elements pretty much all support JS events. Check out w3c for all of them, but look into onchanged or onkeydown for sure. You could use that to call a JS function that updates each variable. Or if you are worried about performance update when the text box loses focus. 

 

One thing is that you should validate all input server side where ever possible, because JS runs client side its very easy to change it to do what you want with the code as a user, and anything more than a contact form needs proper validation 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×