I am looking to change any number entered into a text area by an increment of 4. Example if I type 10 into text field, and then clicked a button, it would become 14, If I typed 10 20 30 40, clicked a button, it would become 14 24 34 44. I suppose I could use regex and replace 10 to 14, but I think there is a better way using a math function or whatever else is a better idea than find replace. I need a text field and a button to run the script. It would be nice to be able to add any increment I want , so instead of 4 I could use 5 and so on. using 5, 10 would become 15 etc.
looking for some help with this, I am sure for lots of you this is very easy to do.
That works great. Now I just need to figure out how to make a text field where I can put the numbers to be changed in. I have not messed with javascript in 12 years, I have forgot a ton of the basics. You have me down the right path though. Thanks
For the form/input/button … that’s um a whole different world (HTML) but much easier than JS in my opinion. Feel free to fork that CodePen and add your HTML. And post back in this topic if you get stuck.
No problem with making the form input or button, just need to make the input numbers run your script, instead of having them in the script beforehand. then output the results instead of the log method you used. I’ll sort it out, you did the hardest part. Thanks again
I’m having no luck trying to make the let input come from a text input. I’ve tried what I feel should of worked, but I am clearly doing something wrong. Instead of let input = ‘10 30 40 50’ I want those numbers to be determined from a text field.
<html>
<head>
<script>
let input = '10 30 40 50'
let addOn = 12 //1. addOn = Number
let splitToArr = input.split(' ') //2. split string into Array
let doMathOnArr = splitToArr.map((i)=>{ //3. Map the array
return parseFloat(i) + addOn //3. Parse each element & add addOn
})
let result = doMathOnArr.join(' ') //4. join back together into a String
</script>
</head>
<body>
<input id="Text1" type="text" value="11" />
<button type="button" id="myBtn" onclick="document.write(result);">Run</button>
</body>
</html>
<html>
<head>
<script>
let input = document.getElementById("Text1")
let addOn = 12 //1. addOn = Number
let splitToArr = input.split(' ') //2. split string into Array
let doMathOnArr = splitToArr.map((i)=>{ //3. Map the array
return parseFloat(i) + addOn //3. Parse each element & add addOn
})
let result = doMathOnArr.join(' ') //4. join back together into a String
</script>
</head>
<body>
<input id="Text1" type="text" value="" />
<button type="button" id="myBtn" onclick="document.write(result);">Run</button>
</body>
</html>
2 things;
First, all of the code needs to be in a function. And that function is called on the button click
Second, let input = document.getElementById(“Text1”).value You selected the correct element but didn’t isolate what you wanted.
Well that worked just fine. Now I want to make the 12 in addOn also changeable by text input.
I am trying to do things just a bit more advanced than I am at the moment. I appreciate the help bigtime and will learn from it. I am catching on and I also suspected I needed a function.
Thanks, this is what I did to finally get it working.
let input = document.getElementById("Text1").value
let addOn = parseFloat(document.getElementById("Text2").value);
let splitToArr = input.split(' ')
let doMathOnArr = splitToArr.map((i)=>{
return parseFloat(i) + addOn
})
let result = doMathOnArr.join(' ')
document.write(result);
};