Value Javascript Input - javascript

how can i transfer the Value from Input 1 in 2 and add some letters?
<script type="text/javascript">
function doit(){
document.getElementById('input2').value=document.getElementById('input1').value;
}
</script>
Input1: 2342
Input2: pcid2342d
Can some one help me?

Just use the + operator to add a string before and after the input value.
<script type="text/javascript">
function doit(){
document.getElementById('input2').value="pcid" + document.getElementById('input1').value + "d";
}
</script>

String concatenation:
document.getElementById('input2').value = "stuff" + document.getElementById('input1').value + "other stuff";
When dealing with numbers you could start by concatenating with empty string to avoid adding numbers together instead of concatenating to strings (because of operator evaluation order):
document.getElementById('input2').value = "" + 1234 + 567 + document.getElementById('input1').value + 89;

Well you seem to have done the work already, all you need is something to click on to execute it:
<button onclick="doit()">click me</button>

Why don't you try something in jQuery?
$("#Anything").click(function() {
$("#Field1").val($("#Field2").val());
});
The "click" is just a assumption =)

Related

Passing a form variable from HTML to Javascript, but with quotes

I'm trying to pass an email address from an HTML form into a javascript snippet to pass to another program.
Form input:
<input name="email" placeholder="Enter Email Address Here" type="email" required="required" id="email_input">
Here's an example of what I'm looking for, note the quotes. In order for the email to pass, it must be within quotes.
<script type="text/javascript">
test({
email: "example#example.com",
});
</script>
Here's what I've tried:
<script type="text/javascript">
test({
email: "document.getElementById("email_input");",
});
</script>
If I'm using a static value like example#example.com, everything works as expected. I'm not able to figure out how to pass the value from the email input field to the "email:" javascript and have it pass though.
Heres what I need: What I have in there document.getElementById("email_input"); is not working, what should this be?
I'm know I'm missing something simple here, just not sure what.
Thanks in advance!
If you're just trying to pass the value of the email input using that test object, this is what you need.
var emailString = document.getElementById("email_input").value;
test({
email: emailString,
});
</script>
You want to grab the value of the input first, and then pass it as a string (assuming you need a string from your first example)
There is two way to get a string from an other typeof element (DOM element, Number, Array, ...) :
<script type="text/javascript">
var elmt = document.getElementById("email_input");
var str1 = elmt.toString();
var str2 = elmt + "";
// alert(str1); and alert(str2) will pop the same result : a string of elmt
</script>
so if you want the entire input as a string you should do :
<script type="text/javascript">
test({
email: document.getElementById("email_input") + ''
});
</script>
if you just want the input value as a string (so the email) you should do :
<script type="text/javascript">
test({
email: document.getElementById("email_input").value + ''
});
</script>
and if you really need to add quotes to a string you also can do :
<script type="text/javascript">
var str= "this is my string";
var withquote= ' " ' + str + ' " ';
//both of the var return a string but one will have a quote and the other won't
alert(str); // pop: this is my string
alert(withquote); // pop "this is my string"
</script>
So in your case :
if you want the entire input as a string with quotes you should do :
<script type="text/javascript">
test({
email: ' " ' + document.getElementById("email_input") + ' " '
});
</script>
if you just want the input value as a string (so the email) with quotes you should do :
<script type="text/javascript">
test({
email: ' " ' + document.getElementById("email_input").value + ' " '
});
</script>
hope I've made myself clear
Feel free to ask if I'm not
have a good one

I need some troubleshooting with my HTML/JavaScript code

I am trying to create code that when you press a button, it will change the value of a variable and replace some text.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p id="unitts">You have 0 unitts</p>
<script type="text/javascript">
var unitt = 0;
function unittIncrease() {
var unittPrev = unitt;
unitt++;
document.getElementById(unitts).innerHTML.replace("You have " + unittPrev.toString() + " unitts.", "You have " + unitt.toString() + " unitts.");
}
</script>
<button id="unittIncrease" onclick="unittIncrease();">Digitize Unitt</button>
</body>
</html>
When I press the button, nothing happens to the text.
I don't know why this does not work.
Please help me!
EDIT: I am only 11 years old,
please don't throw your wizard
code at me.
maybe you should remove your button system and add a while loop that
automatically add a unit but waits one second with a setInterval
function
I think you should write the js code like this
document.getElementById('unitts').innerHTML = "You have"....
Instead of:
document.getElementById(unitts).innerHTML.replace("...")
Your JavaScript should be (note the unitts wrapped in quotes and the full stop removed):
document.getElementById('unitts').innerHTML = "You have " + unitt + " unitts";
Instead of:
document.getElementById(unitts).innerHTML.replace("You have " + unittPrev.toString() + " unitts.", "You have " + unitt.toString() + " unitts.");
In the latter, it is looking for the non-existent variable unitts instead of the string 'unitts'. Also, you are looking for the text You have x unitts. which cannot be found because in your HTML, it is just You have x unitts without the full stop.
Edit
See this plnkr.
Apart from the issues that the other answer mentions, by calling .replace method on the .innerHTML property of the element, the content of it doesn't change. You should reset the property by using the returned value of the method call:
el.innerHTML = el.innerHTML.replace(...);
Also, as you are trying to increase a number, instead of replacing all the characters, you can just replace the numeric part:
var unitts = document.getElementById('unitts');
function unittIncrease() {
unitts.textContent = unitts.textContent.replace(/\d+/, function(n) {
return +n + 1;
});
}
https://jsfiddle.net/h6odbosg/

How to test if a string has only spaces in Javascript?

I have a textarea and I need to test if the user put a text like " ", or only spaces in it or only " ", I can't accept only spaces, but I can accept " Hi !!". How can I do this in Javascript?
Just trim it and the length will be 0 if it is all spaces.
strname.trim().length == 0
You check it like this: demo on JSexample
<script>
var text = ' '
if(text.match(/^\s*$/)){
alert('contains only spaces!')
}
</script>
Be careful, some browsers don't support trim() function. I'd use like this:
if (!!str.replace(/\s/g, '').length) {
alert('only spaces')
}

Javascript function parameter character escape

I need to pass a variable to a JavaScript function, but I have a little trouble. In the .cs file, I have written:
string id = "some'id";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
As you can see there is an ' (single quote) in the id. Is there any way to work around this issue?
Escape ' with a \ (backslash). For example,
console.log('string with \'');
Escape your string for such kind of characters"/","\","'"
example
string id = "some/'id";
You should escape your string , which would lead to :
id = "some\'id";
<script type="text/javascript">
function myFunction(someid) {
someid = someid.replace('#', '\'');
alert(someid);
}
</script>
in Your code
string id = "some'id".Replace("'","#");
this.Controls.Add(new LiteralControl("<input type=\"button\" value=\"Test\" onclick=\"myFunction('" + id + "');\">"));
Hope this will Helps you..

How would I remove from spaces from a search form using jQuery?

I've replaced the submit URL from the search form with this jQuery snippet:
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
window.location.href = "/search-" + $('.search-form input:text').val() + "-" + "keyword"+ "-"+ "keyword2/" + $('.search-form input:text').val() + ".html";
return false;
});
});
</script>
This works fine and turns the URL into a nice SEO cleaned URL. But how can I replace the spaces?
When someone types in "search me" the URL looks like /search-search me-keyword-keyword2/search me.html with spaces. With + or - it would look much better. I know of str_replace from PHP, but how would I go about this in jQuery?
There's a native JavaScript function called encodeURIComponent that's intended to do exactly what you need.
window.location.href =
"/search-" +
encodeURIComponent($('.search-form input:text').val()) +
"-" + "keyword" + "-" + "keyword2/" +
encodeURIComponent($('.search-form input:text').val()) +
".html";
Method 1: Using Replace
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
var value = $('.search-form input:text').val();
value = value.replace(' ', ''); // replace
window.location.href = "/search-" + value + "-" + "keyword"+ "-"+ "keyword2/" + value + ".html";
return false;
});
});
</script>
Method 2: Encoding URL
<script type="text/javascript">
$(document).ready(function() {
$('.search-form').submit(function() {
// encode url
var value = encodeURIComponent($('.search-form input:text').val());
window.location.href = "/search-" + value + "-" + "keyword"+ "-"+ "keyword2/" + value + ".html";
return false;
});
});
</script>
Note that replace method would work even in JQuery because Jquery is simply library of javascript :)
http://www.w3schools.com/jsref/jsref_replace.asp
replace() method is native javascript. That'll get you what you want.
You could remove spaces with using mod_rewrite. It’s quite useful way. You can also remove spaces from URLs by replacing spaces by %20, but that only helps with spaces and won't escape other characters. What you really need to do is use a URL-escaping function.

Categories