This is a really stupid question.
I have a javascript string variable for a name, and i want to display it where it says user like this:
"Hello, user!" --> "Hello, Chris!
Surely you could have found this answer out easily yourself :p
Put the name in a span and give it an ID
Hello <span id="name"></span>
Then set the text using getElementByID
var name = "Chris";
document.getElementById('name').innerHTML = name;
var user_name = 'Chris';
document.writeln("Hello, " + user_name);
I think document.write plus some string concatenation are what you're looking for:
var user = "Chris";
document.write("Hello, " + user + "!");
Something like this will do the trick.
var user = 'Steve';
document.write('hello ' + user);
If you need to target an element, you can use the usual methods, such as:
var user = 'Steve';
var thisOne = document.getElementById('thisOne');
thisOne.innerHTML = ('hello ' + user);
May as well throw in a jsfiddle so you can play around an experiment.
This is example of dislay string into span tag.
'+' operator uses for string concatenation.
<html>
<head>
<script>
var name = 'Chris';
var field = document.getElementById('show_string');
field.innerHTML( '"Hello, '+ name + '!"' );
</script>
</head>
<body>
<span id='show_string'></span>
</body>
</html>
You can concatenate the user's name with the rest of the string you want to display like so:
<p id="hello"></p>
<script type='text/javascript'>
var user_name = "Chris";
var hello_string = "Hello, " + user_name;
document.getElementById("hello").innerHTML = hello_string;
</script>
Related
I have limited knowledge of javascript,so hoping someone can help.
Tried w3 schools & stacko using Document.
This is a dumb question.
We receive a variable value that outputs string without quotes.The string has spaces.
In the example below is it possible to add quotes to my string.
how do i use a input value without quotes and parse it as string?
myvar = John doe
is it possible to add Quotes to a value assigned to a variable?
<html>
<body>
<div id="param">my string</div>
<div id="base64param"></div>
<div id="encodeparam"></div>
<div id="myframe"</div>
<div id="myframe2"</div>
<iframe id="myframe" src="" width="400" height="800" frameborder="0">
</iframe>
<script>
window.onload = function myFunction() {
var str = document.getElementById("param").innerHTML;
var base64param = window.btoa(str);
document.getElementById("base64param").innerHTML = "base64:" + base64param;
var encodeparam = encodeURIComponent(str);
document.getElementById("encodeparam").innerHTML = "encoded:" + encodeparam;
var appurl = "http://localhost/index.jsp?param=";
var furl = appurl + str;
document.getElementById("myframe").innerHTML = "URLwithout encode:" + furl;
var burl = appurl + base64param;
document.getElementById("myframe2").innerHTML = "URLwith encode:" + burl;
}
</script>
</body>
</html>
If you have an existing variable, and want to add quotes:
var varWithQuotes = "\"" + oldVar + "\"";
The slash is an escape character and the plus sign is how to concatenate strings.
I'm trying to build a function, that receives a string with this format:
"hello wor**"
The * could be anywhere on the string.
It should return:
<span>hello wor</span><input type='text'></input>
So the string could be "hel** wor*d" also
and the return should be:
<span>hel</span><input type='text'> <span>wor</span><input type='text'><span>d</span>
I could do it easily with a loop on each char, but I'm looking for more elegant solutions.
I think that it could be solved with a regex, and using replace I got the "*" covered:
var text = "hello wor**";
text.replace(/\*+/g, "<input type='text'></input>");
I have not yet found a way of capturing the remaining text to render the
<span>
You're not using the result of the replace function. Try this:
var text = "*hel** wor*d*";
var element = text.split(/\s*\*+\s*/g);
element = "<span>"+ element.join("</span><input type='text'><span>") + "</span>";
element = element.replace(/<span><\/span>/g, "");
console.log(element);
'hello wor**'.replace(/\*+/g, "<input type='text'></input>");
This returns hello wor. All you have to do is concatenate the string with the rest of the data you want, like so:
var text = "hello wor**";
text = '<span>' + text.replace(/\*+/g, '') + '</span><input type=\'text\'></input>';
<html>
<head>
</head>
<body>
<span id="hi">hello wor**</span>
</body>
</html>
i use jquery in this
$( document ).ready(function() {
var texty = $('#hi').text();
$('#hi').replaceWith(texty.replace(/\*+/g, "<input type='text'></input>"))
});
I'm new to JS and I am trying to make two random strings that were selected by using math.random() and put them into my p tag so that I could end up with something like 'Storm Breaker' or 'Castle Eater', but it seems I can only display the first name (fname) or the last name (lname). I tried to do
document.getElementById("print").innerHTML=fname,lname;
That didn't work so I also tried:
document.getElementById("print").innerHTML=fname;
document.getElementById("print").innerHTML=lname;
But as you could tell that would just change where the first name would be into the last name.
<h1 class="title">Welcome to Tom's Random Name Generator!</h1>
<button type="button" onclick="myFunction();">Generate!</button>
<p id="print"></p>
<script>
function myFunction() {
var fnamel = ['Storm','Wind','Castle','Chocolate','Savage'];
var lnamel = ['Breaker','Eater','Smasher','Killer','Fury'];
var fname = fnamel[Math.floor(Math.random()*fnamel.length)];
var lname = lnamel[Math.floor(Math.random()*lnamel.length)];
document.getElementById("print").innerHTML=fname;
}
</script>
Change like this :
document.getElementById("print").innerHTML=fname + " " + lname;
Final code :
<html>
<head>
<style>
</style>
</head>
<body>
<h1 class="title">Welcome to Tom's Random Name Generator!</h1>
<button type="button" onclick="myFunction();">Generate!</button>
<p id="print"></p>
<script>
function myFunction() {
var fnamel = ['Storm','Wind','Castle','Chocolate','Savage'];
var lnamel = ['Breaker','Eater','Smasher','Killer','Fury'];
var fname = fnamel[Math.floor(Math.random()*fnamel.length)];
var lname = lnamel[Math.floor(Math.random()*lnamel.length)];
document.getElementById("print").innerHTML=fname + " " + lname;
}
</script>
</body>
</html>
You can concatenate strings in javascript using the plus operator.
document.getElementById("print").innerHTML=fname + ' ' + lname;
These are my Json arrays:
{"0":"1","id":"1","1":"2015-01-11 12:30:45","DateTimeCreated":"2015-01-11 12:30:45","2":"Pending Confirmation","status_desc":"Pending Confirmation","3":"benjiwjh","username":"benjiwjh"}
{"0":"4","id":"4","1":"2015-02-11 09:09:09","DateTimeCreated":"2015-02-11 09:09:09","2":"Pending Confirmation","status_desc":"Pending Confirmation","3":"LSH","username":"LSH"}
{"0":"7","id":"7","1":"2015-12-03 18:30:00","DateTimeCreated":"2015-12-03 18:30:00","2":"Unresolved","status_desc":"Unresolved","3":"SWJH","username":"SWJH"}
{"0":"12","id":"12","1":"2014-12-03 12:10:30","DateTimeCreated":"2014-12-03 12:10:30","2":"Resolved","status_desc":"Resolved","3":"benjiwjh","username":"benjiwjh"}
{"0":"14","id":"14","1":"2014-12-03 12:10:30","DateTimeCreated":"2014-12-03 12:10:30","2":"Resolved","status_desc":"Resolved","3":"CYJM","username":"CYJM"}
How am I supposed to use these to display my code in an HTML file?
I have a function to show the data but it does not work:
function showData(response) {
var data = JSON.parse(response);
var id = data.id;
var DateTimeCreated = data.DateTimeCreated;
var status_desc = data.status_desc;
var username = data.username;
myText.textContent= id + DateTimeCreated + status_desc + username;
}
I think you should have a div tag in your html template.
For example, in your HTML template, there should be a scope of codes:
<div id="sometext">
</div>
And in your JS script,
var textContent = id + DateTimeCreated + status_desc + username
sometext = document.getElementById("sometext");
sometext.innerHTML(textContent);
For more info, please visit http://www.w3schools.com/js/js_output.asp
Hope it would be helpful
Based on the question, I'm not entirely sure when you are using the showData function. However, here's a jsfiddle that shows that it works: https://jsfiddle.net/qyy2nvtz/1/
<p id='hello'></p>
var myText = document.getElementById('hello');
You need to get an element before you can display something in it. So, I've initialized 'myText' with a paragraph element with id 'hello'.
Additionally, make sure that the response that is being passed in to showData is a string before you parse it. If it's already an object, then it won't be parsed.
Here you have an example, you need a div element to refer like "js":
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<div id="js">
</div>
</body>
<script type="text/javascript">
var myText= document.getElementById("js");
var data = JSON.parse(response);
var id = data.id;
var DateTimeCreated = data.DateTimeCreated;
var status_desc = data.status_desc;
var username = data.username;
myText.textContent = id + DateTimeCreated + status_desc +username;
</script>
</html>
Further reading: http://www.w3schools.com/dom/prop_element_textcontent.asp
This is for some homework of mine. Javascript has thus so far been the most difficult for me to grasp. Here's a copy of my assignment:
Copy the following code into new file.
<html>>
<body>
<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;
document.write(namAr + "<br /> Length:" + namArLen);
</script> </body>
</html>
When you execute this code you will see that after you entered your name it was loaded into namAr array. Your output will look like this:
C,o,l,o,r,a,d,o, ,M,a,r,k, ,U,d,a,l,l
Length: 19
Your task:
Add a small iteration that will display your name in reverse order. For example if senator’s state and name as I entered as:
Colorado Mark Udall
your output will be
C,o,l,o,r,a,d,o, ,M,a,r,k, ,U,d,a,l,l
Length: 19
lladU kraM odaroloC
I'm trying to achieve the bold part. They check our coding. I'm not looking for an answer as much as I am a sort of walkthrough or explaination. Anything help, thanks!
RESOLVED
Figured it out, thanks! All great answers- you guys are so smart!
<html>
<body>
<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;
document.write(namAr + "<br /> Length:" + namArLen);
var i;
for (i = namArLen-1; i >=0;i--)
{
var result = document.write(namAr[i]);
}
</script>
</body> </html>
you can use loop
Instead of
var namAr = namIn.split("");
try
var namAr = namIn.split("").reverse();