html forum and javascripty [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get an text field text in to an array? Is it possible to get a text from text field and put it in to character array to select each Alphabet and use it in different places?
Like I have a words
Computer
<input type="text" name="name" />
I need it like in different Block Like Name Speel
[C] [o] [m] [p] [u] [t] [e] [r]
Any help appreciated.

What you might want is the following JavaScript:
"Computer".split("")

I think that the main problem is this part: "select each Alphabet and use it in Different Places Like..." so the solution is to use .charAt() function.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "How are you doing today?";
var res = str.charAt(0);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Related

How do I switch html pages with JavaScript if I don't have the Url [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 hours ago.
This post was edited and submitted for review 17 hours ago.
Improve this question
I have them both as HTML files in my repo. The button is just meant to switch from pages for now. I will add the function for the inputs and conditions later. I just want to get in working before I add anything. There are also no errors that or popping up. I cannot use a tag as I need to change the URL to pass data through it, so I need to have it in JS so I can change it with the data from the input.
The plan is to have the user put in their name in a text input, and have it inside the Url, then have the 2nd page process the name and output a text and photo.
This is the code that I have tried.
const nameButton = document.getElementById("nameBtn");
$(nameButton).click(function(){
preventDefault()
function change_page(){
window.location.replace("page-two.html");
};
change_page()
});
This is the Html:
<input class="font" type="text" id="inputName" value="Type in
your name..."></input>
<button class="fonts" id="nameBtn">Ready?</button>
</form>
</div>
</body>
<script src="scripts/script.js"></script>
</html>
Short answer, only replace the pathname
window.location.pathname = 'page-two.html'
In your case:
const nameButton = document.getElementById("nameBtn");
nameButton.addEventListener('click', function (event) {
event.preventDefault()
window.location.pathname = "page-two.html";
});

How do I change text within <div> tags with Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need some help on editing elements which are in HTML with JavaScript. Is it possible to change the "text" with javascript? I've tried using document.getElementById but I don't think I'm doing it correctly.
Get the elemet with a query like you would use in css:
var element = document.querySelector('div.header');
Change the content using the textContent property:
element.textContent = "New Content;"
Adding image to content:
document.querySelector('div.header').innerHTML = "<img src='smiley.gif' alt='Smiley face' height='42' width='42'>";
Simple example with "press me" button that calls a function to change text
<html>
<script>
function changeText(){
document.getElementById("header").innerHTML = "changed text";
};
</script>
<body>
<div id="header">text</div>
<button onclick="changeText()">press me</button>
</body>
<html>
<script>
function changeText(){
document.getElementById("header").innerHTML = "changed text";
};
</script>
<body>
<div id="header">text</div>
<button onclick="changeText()">press me</button>
</body>
</html>
if querySelectorAll or getElementsbyClassName there are many ways to alter the text.
My answer is that you must think about your structure of the HTML Document.
You can use every ID only once. It is better to use data-lb="detail" etc. and classes for the divĀ“s
Please see this not as an answer only as an tip.
You can try something like this -
<button id="click">
CLICK
</button>
<div class="header" id="textChange">Text</div>
And then in your JS -
$( "#click" ).click(function() {
$( "#textChange" ).replaceWith( "<h2>New heading</h2>" );
});

javascript/html: Remove element by ID on button click [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In short, what I want to do is make it so when I click a button in my HTML document, I want that button to subsequentely be removed by it's ID, using javascript if possible. I've been searching for a solution with no success, can anyone help me out here?
In your HTML:
<button onclick="deleteSelf(this)">Click me</button>
In your JavaScript:
function deleteSelf(button) {
button.remove();
}
This works:
https://jsfiddle.net/00p4oLfy/
document.getElementById('test').remove();
<p id="test">This is a test</p>
<p id="test2">This is another test</p>
Html
<div>
<button id="ClickMe">Click Me</button>
</div>
Javascript
document.getElementById('ClickMe').remove();

Change text in class with deferent text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible with jQuery to append a different word at the end of each node with a same class ?
For example:
<p class="xyz">Some text</p>
<p class="xyz">Some another text</p>
<p class="xyz">Text somewhere</p>
would then be:
<p class="xyz">Some text word1</p>
<p class="xyz">Some another text word2</p>
<p class="xyz">Text somewhere word3</p>
Thanks
Use:
$('.xyz').each(function(i){
$(this).html($(this).html()+" word"+(i+1));//or $(this).text($(this).text()+" word"+(i+1));
});
Working Demo
Look at this:
$('.xyz').each(function(i){
var count = i+1;
$(this).html($(this).html()+ "word" + count)
});
http://jsfiddle.net/y9043kdn/1/

How to show spaces from textarea [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I'm saving data in a text area on page 1 then showing it on page 2 in a div. However if I write in the textarea
A
B
C
D
It shows it in the div (on page 2) as
A B C D
How do I maintain the line breaks?
Use the CSS property white-space to accept \n as new lines.
div {
white-space: pre;
}
JSFiddle
You can do it with the below code:
function setVal() {
var content = document.getElementById('test').value;
document.getElementById('result').innerHTML = content.replace(/\n/g, '<br/>');
}
Explanation: Basically, the need is to convert the \n characters to their equivalent HTML line breaks using <br/>.
Demo
You need to use this function nl2br() and FYI Don't use nl2br when you save it to the database. Use it when you're displaying the text in HTML.
I would use the <pre> tag which is used for preformatted texts like this on your second page.
An example of which would be this:
<?
echo '<pre>'.$_POST['textarea'].'</pre>';
It preserves both spaces and linebreaks.
$textarea = strip_tags($_POST['textarea']);
echo '<textarea>'.nl2br($textarea).'</textarea>';
nl2br = new line to <br>, so it'll insert <br><br> for every carriage return.
You should use nl2br when displaying the value in div
Online Demo
Here is the code so that you can run it check.
<form>
<textarea name="message" rows="10" cols="50" placeholder="Enter your message here"><?=$_REQUEST['message']?></textarea><br>
<button name="submit" value="submit">Submit</button>
</form>
<h2>Your message will display here</h2>
<div><?=nl2br($_REQUEST['message'])?></div>
<h3>Always remember that don't store nl2br value in database.</h3>

Categories