Use jquery Input value window prompt in html - javascript

What I'm trying to do is have a window prompt / alert box pop up with a text input. I want the value of the text in my jQuery code to be used in place of the word YOU in the a tag in my html document.
jQuery code I'm using
$(document).ready(function() {
var userName = window.prompt("Please Enter Your Name", "Text");
});
My html
<body>
<center><h1 class="Logo">Welcome to<br>an epic story adventure<br>starring <a>YOU!</a></h1></center>
<p></p>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/name.js"></script>
</body>
so basically, I wanna replace YOU! with the input value of the window prompt

You'd just need to add this line after the userName JavaScript line you have:
$('h1.Logo a').text(userName);
On a side note, don't use the <center> element; it's not valid anymore. Use CSS instead. Also, a <span> might be more appropriate than an <a> tag in your example.

Related

Changing Text of HTML Button Using Outside JS Variable

In my main HTML file, I create a variable using this script:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
//Creation of answerjson not shown
var choiceOne = answerjson[0].choice;
});
Later in my HTML, I create a button within a div using this code:
<div id= "one"><button class="button" id = "b1"></button></div>
How can I display the value of choiceOne as the label on button b1? I've searched online, but only found answers to this problem when the button is not in a div.
var choiceOne = "Some Text";
// jQuery
$("#b1").text(choiceOne);
// Regular JavaScript
document.getElementById("b2").innerHTML = choiceOne;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one"><button class="button" id="b1"></button></div>
<div id="two"><button class="button" id="b2"></button></div>
Like this:
$('#b1').html(choiceOne);
but you need to do this in
$(document).ready(function() { });
so you are sure all of the DOM is ready and loaded ... not sooner
Access the button from inside your javascript and change the text on it using the following command:
document.getElementById("b1").innerHTML = choiceOne;
This is in regular JS. You can also use JQuery to select the container.

Javascript get text from input and copy it to a span

I'm trying to make a template for a copy and paste it have a input and I want to put some text in it and press a button that says update and copy the text from the input to somewhere in the template
My attempt:
<!DOCTYPE HTML>
<html>
<head>
<title>Donovan_D Minecraft - Youtube Description
Template</title>
</head>
<body>
<!--Start Template-->
<div>===================================<br>
My Channel:<br>
https://youtube.com/c/DonovanDMinecraft<br>
===================================<br>
Twitch:<br>
http://twitch.tv/donovan_dmc<br>
Twitter:<br>
http://twitter.com/Donovan_DMC<br>
===================================<br>
Sub2Janabell:<br>
https://youtube.com/channel/UC0NTNba35ADUstIoLm
YLiiA<br>
===================================<br>
My <span style="text-decoration:underline;" id="v">1.9,
1.10, 1.11</span> Skyblock/Guilds Server:<br>
IP: play.mcpsb.co<br>
Server store:<br>
http://store.mcpsb.co<br>
Server Website:<br>
https://www.mcpsb.co<br>
===================================<br>
Thanks for <span style="text-decoration:underline;"
id="sub">970</span> Subscribers! make sure to like,
comment, subscribe, and Stay Awesome!
===================================<br>
<!--End Template-->
<hr>
NOT PART OF TEMPLATE<br>
</div>
<b>Version:</b><br>
Change: <input id="vr"><br>
<button onclick="ver()">Update Version</button><br>
<hr>
<b>Subscriber Count:</b><br>
Change: <input id="cs"><br>
<button onclick="csub()">Update Subscriber
Count</button><br>
<hr>
<script type="text/javascript">
function ver() {
document.getElementById("v").textContent =
document.getElementById("vr").value;
}
function csub() {
document.getElementById("sub").textContent =
document.getElementById("cs").value;
}
</script>
</body>
</html>
[I'm trying to do it twice]
Am I doing somthing wrong here?
Solved, one more thing, is there a way to see if the input is the same as is in the span or if the input is empty? And I only want numbers used in them... is there a way to block other characters/replace themy once they are typed?
Aswell for the subscriber Ammount I want to get live numbers from akshatmittal.com or some other live counts service for youtube Subscribers. Is there a way to get the live Subscriber Count without php, ajax or anything besides javascript and html
The problem is that you are trying to get the data out of your text boxes with .innerHTML. Form-field values need to be gotten with .value.
Additionally, if the data being typed into the text boxes will not contain any HTML that needs to be parsed, you should use .textContent to set that data into your <span>s instead of .innerHTML since there isn't any HTML.
.value is for getting/setting form-field values (checkboxes, radio buttons, text boxes, etc.) or attribute values.
.innerHTML is for getting/setting non-form-field element content that will contain HTML markup. When using this to set content, any HTML in the value being set will be parsed by the browser.
.textContent is for getting/setting non-form-field element content that will not contain HTML markup. HTML markup will be ignored. .textContent is the better choice when no HTML will be gotten/set because it performs better than .innerHTML (doesn't have to parse anything).
<!DOCTYPE HTML>
<html>
<head>
<title>Donovan_D Minecraft - Youtube Description Template</title>
</head>
<body>
<h3>To View HTML Click Here </h3><br>
<!--Start Template-->
<div>===================================<br>
My Channel:<br>
https://youtube.com/c/DonovanDMinecraft<br>
===================================<br>
Twitch:<br>
http://twitch.tv/donovan_dmc<br>
Twitter:<br>
http://twitter.com/Donovan_DMC<br>
===================================<br>
Sub2Janabell:<br>
https://youtube.com/channel/UC0NTNba35ADUstIoLmYLiiA<br>
===================================<br>
My <span style="text-decoration:underline;" id="v">1.9, 1.10, 1.11</span> Skyblock/Guilds Server:<br>
IP: play.mcpsb.co<br>
Server store:<br>
http://store.mcpsb.co<br>
Server Website:<br>
https://www.mcpsb.co<br>
===================================<br>
Thanks for <span style="text-decoration:underline;" id="sub">970</span> Subscribers! make sure to like, comment, subscribe, and Stay Awesome!
===================================<br>
<!--End Template-->
<hr>
NOT PART OF TEMPLATE<br>
</div>
<b>Version:</b><br>
Change: <input id="vr"><br>
<button onclick="ver()">Update Version</button><br>
<hr>
<b>Subscriber Count:</b><br>
Change: <input id="cs"><br>
<button onclick="csub()">Update Subscriber Count</button><br>
<hr>
<script type="text/javascript">
// Get/set form-field values using the .value property
// Get/set non-form-field values with either .textContent or .innerHTML
function ver() {
document.getElementById("v").textContent = document.getElementById("vr").value;
}
function csub() {
document.getElementById("sub").textContent = document.getElementById("cs").value;
}
</script>
</body>
</html>
Try the following script changes to get the input value and change the textContent of the target element.
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
<script type="text/javascript">
function ver() {
document.getElementById("v").textContent = document.getElementById("vr").value;
}
function csub() {
document.getElementById("sub").textContent = document.getElementById("cs").value;
}
</script>

Cannot get the hidden tag value

Here is my code:
<html>
<head>
<script type="text/javascript">
var x= document.getElementById("2").value;
document.getElementById("1").innerHtml = x;
</script>
</head>
<body>
<p hidden="hidden" id="2">This paragraph should be hidden.</p>
<p>This is a visible paragraph.</p>
<p><b>Note:</b> The hidden attribute is not supported in IE.</p>
<p id="1"></p>
</body>
</html>
in this code i have a hidden tag as you can see. I want that the javascript code read text value of the p tag with an id 2 and then print the same value to other <p> tag wiht id="1". But this is not working. Earlier i even tried to use nodeValue but also this is not working and when i checked out in google developer tool then it was showing an error as following:
Cannot read property 'value/nodeValue' of null
please note:
after a quick experiment i noted that after adding a event handler <body onload="y();>" there was no error but there was no expected result!
please help!
hidden is an input element type, not a p attribute:
<input type="hidden" id="2" value="This input should be hidden." />
There are three problems:
there is no innerHtml, innerHTML is the correct syntax.
the hidden "p" does not have a value, it is not an input field. use innerHTML for accessing it.
your javascript code runs before the browser knows about paragraps, so they don't exist when you want them to be accessed. put javascript after the paragraphs or run the code after the page is loaded.
this should work:
<html>
<head>
</head>
<body>
<p hidden="hidden" id="2">This paragraph should be hidden.</p>
<p>This is a visible paragraph.</p>
<p><b>Note:</b> The hidden attribute is not supported in IE.</p>
<p id="1"></p>
<script type="text/javascript">
var x= document.getElementById("2").innerHTML;
document.getElementById("1").innerHTML = x;
</script>
</body>
</html>
Don't use numbers for ID.
Try something like <p id="hello"></p>
I think you need to change your tag to then you can set a CSS class with .hidden { display:none; }.
Wrap your Javascript in a function and call it when you need to or go back to your
Also as Maaz said, try not to use numbers in your ID's.
var hiddenValue = document.getElementById('2').innerHTML;
document.getElementById('1').innerHTML = hiddenValue;
The problem with this (and if you try and style it also) is that classes and ID's should not start with (or include) numbers.
Rename your ID's to one and two and then update your javascript accordingly.
e.g
<p id="one">Some stuff</p>
Also hidden cannot be used with a p element as it's for inputs only.
You're better off using display:none; in CSS.
If you NEED to access it via css as a number, you can use
[id='1']{
/*code*/
}
but your javascript still wont work.
As James has pointed out, using numbers for ID's is perfectly valid in HTML5.

jQuery .append() not appending to textarea after text edited

Take the following page:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"/>
</head>
<body>
<div class="hashtag">#one</div>
<div class="hashtag">#two</div>
<form accept-charset="UTF-8" action="/home/index" method="post">
<textarea id="text-box"/>
<input type="submit" value ="ok" id="go" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$(".hashtag").click(function() {
var txt = $.trim($(this).text());
$("#text-box").append(txt);
});
});
</script>
</body>
</html>
The behavior I would expect, and that I want to achieve is that when I click on one of the divs with class hashtag their content ("#one" and "#two" respectively) would be appended at the end of the text in textarea text-box.
This does happen when I click on the hash tags just after the page loads. However when I then also start editing the text in text-box manually and then go back to clicking on any of the hashtags they don't get appended on Firefox. On Chrome the most bizarre thing is happening - all the text I type manually gets replaced with the new hashtag and disappears.
I probably am doing something very wrong here, so I would appreciate if someone can point out my mistake here, and how to fix that.
Thanks.
2 things.
First, <textarea/> is not a valid tag. <textarea> tags must be fully closed with a full </textarea> closing tag.
Second, $(textarea).append(txt) doesn't work like you think. When a page is loaded the text nodes inside the textarea are set the value of that form field. After that, the text nodes and the value can be disconnected. As you type in the field, the value changes, but the text nodes inside it on the DOM do not. Then you change the text nodes with the append() and the browser erases the value because it knows the text nodes inside the tag have changed.
So you want to set the value, you don't want to append. Use jQuery's val() method for this.
$(document).ready(function(){
$(".hashtag").click(function(){
var txt = $.trim($(this).text());
var box = $("#text-box");
box.val(box.val() + txt);
});
});
Working example:
http://jsfiddle.net/Hhptn/
Use the val() function :)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div class="hashtag">#one</div>
<div class="hashtag">#two</div>
<form accept-charset="UTF-8" action="/home/index" method="post">
<textarea id="text-box"></textarea>
<input type="submit" value ="ok" id="go" />
</form>
<script type="text/javascript">
$(document).ready(function(){
$(".hashtag").click(function(){
var txt = $.trim($(this).text());
$("#text-box").val($("#text-box").val() + txt);
});
});
</script>
</body>
</html>
Does that help?
The reason append does not seem to work is because the value of the textarea is made up of the child node, but by treating it as multiple seperate nodes the screen won't update, according to my Firebug. Firebug will show me the updated child nodes, but NOT the text I typed manually into the textarea, whereas the screen shows me the manually typed text but not the new nodes.
You can reference by value of textarea.
$(document).ready(function () {
window.document.getElementById("ELEMENT_ID").value = "VALUE";
});
function GetValueAfterChange()
{
var data = document.getElementById("ELEMENT_ID").value;
}
works fine.
if(data.quote) $('textarea#message').val($('textarea#message').val()+data.message +' ').focus();

Get updated value from a textarea and insert it into a div with jQuery?

here's an easy one (that I'm struggling with)! I have a textarea, a button, and an empty div. All I want to do is insert the updated contents of the textarea into the div onClick of the button. Here's what I've got so far:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function get_text() {
$("#preview").replaceWith( $("#editor").val() );
}
</script>
</head>
<body>
<form>
<textarea name="editor" id="editor">GrumbleCakes</textarea>
<input type="button" value="Preview" onclick="get_text();" />
</form>
<div id="preview"></div>
</body>
</html>
It works the first time you click the button... with the value that was in the textarea on page load ("GrumbleCakes"), but that's it. It won't work with any updated text.
You can set the innerHTML or text content of the preview div by using the html or text functions:
$("#preview").html($("#editor").val());
.replaceWith actually replaces the DOM element. So the div is removed and replaced with the text. Subsequent calls to the function will no longer find the div, since it's been removed.
I think you want to use
.html($("#editor").val()).
Both
$('#preview").html($("#editor").val())
and
$("#preview").text( $("#editor").val())
should work.
However, .html will allow anyone to inject html or javascript into your site leaving it wide open for cross-site scripting attacks...
jikes!!
man you are replacing div with the contents of the textarea. use this function instead:
function get_text(){
var t=$("editor").val();
$("#preview").text(t);
}

Categories