This question already has answers here:
How do I copy to the clipboard in JavaScript?
(27 answers)
Closed 8 years ago.
If I have e.g. 20 lines of text, e.g.
<p class="textlist">Cheese</p>
<p class="textlist">Water</p>
<p class="textlist">Earth</p>
<p class="textlist">Helicopter</p>
<p class="textlist">Rabbit</p>
I wondered if anyone might be able to advise please, how I might be able to have a button on the page which a user can click, which copies the text values to the clipboard, so that all the user gets in their clipboard are the p innerHTML values in a list split by line breaks, e.g.
Cheese
Water
Earth
Helicopter
Rabbit
The example above is just a simple example, rather than the finished product I'l be working with, just something to use as an example to demonstrate the point.
I was thinking it might be possible using Javascript / jQuery / accessing the DOM, but I don't know how one would do it.
Use .map in Jquery to collect all P tag data and append the text to some other element
var data = $("p").map(function () {
return "<div>" + $(this).text() + "</div>";
}).get();
$(".getDiv").append(data);
DEMO
Related
This question already has answers here:
Get Selected HTML in browser via Javascript
(3 answers)
Closed 4 years ago.
I wonder how to obtain the element tags of selected text along multiple tags.
For example,
<div>
<p>I am (selection start)a boy</p>
<p>You are a girl</p>
<p>We are(selection end) friends</p>
</div>
When I print out selection.getRangeAt(0).commonAncestorContainer.tagName, it shows "DIV".
However, I would like to obtain "p", "p", "p" for "a boy", "You are a girl", "We are", respectively.
Is it possible in Chrome browser? If it is, please help me!
Thank you for your time:)
I am not sure if I understood your question but with query selector you can do something like this:
document.querySelectorAll(“div > p”)
That will return all the “p” inside the div
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
I'm trying to achieve something like templating with JS. The example use-case is, I have page with two objects, rectangle and circle. When I click on rectangle it should show me html button with text DELETE RECTANGLE.
In reality, I will call ajax get on template file (panel.htm) to get what is inside this file. Then I create div element using JS. Then I populate innerHTML with panel.htm(template). In this template is .
What I want is to change this button inner html inside the created and populated div. Im using getByClass because ById is not defined for element.
BUT changes to the button are not propagated/copied to original div.
// Template data
var html = `<div>
<button class="delBtn" type="button">OLD TEXT</button> <br>
</div>`;
// creating new div.
var div = document.createElement('div');
// putting template to new div
div.innerHTML = html;
// accessing the button from the inserted template
div.getElementsByClassName('delBtn').innerHTML = "NEW BUTTON TEXT";
// There I would expect to get div with button inside with NEW TEXT
console.log(div);
// In production here will be some insertion to page etc. This is just bare example of code.
I know this is possible to solve using some template framework. But I would like to stay to clean JS if possible. Also this is new for me, so I would like to learn little bit, and I did not manage to find some answer anywhere.
This seems to me like pure basic stuff so I hope this question is not redundant.
The getByClass returns always an HTMLCollection so you might want to get the first like
div.getElementsByClassName('delBtn')[0].innerHTML = "NEW BUTTON TEXT";
This will work!
Also if you want to try it do
document.body.appendChild(div);
#thatOneGuy is right you will need to select the index of the class you are trying to get but also your div variable will display
<div>
<div>
<button class="delBtn" type="button">OLD TEXT</button> <br>
</div>
</div>
This question already has answers here:
How to strip HTML tags with jQuery?
(5 answers)
Closed 8 years ago.
Consider, this is some response data I got from Ajax.
<table width=100%><tr><td>This content is applicable for users.
</td</tr><tr><td align='center'><button class='btn' onclick='hidethis();'
type='button'>OK</button></td</tr></table>
Here, I need to get only the contents This content is applicable for users. by splitting the html tags. Normally, we use like
var tokens = this.value.split(" ");
Based on Array tokens[0], tokens[1], we will get the values.
How can I get the values by splitting html <table> tags.
Any help would be highly appreciated.
I want to use only jquery to achieve this. And it should be simplified.
as you have received the data as string, you can create the constructor and then get required text from first td element:
var response="<table width=100%><tr><td>This content is applicable for users.</td</tr><tr><td align='center'><button class='btn' onclick='hidethis();' type='button'>OK</button></td</tr></table>";
var tdcontent=$(response).find('td:eq(0)').text();
NOTE: You need to reformat the DOM to make above solution work. you have broken html in string. td elements are not closed properly.
This question already has answers here:
Load .txt file using JQuery or Ajax
(4 answers)
Closed 8 years ago.
I am making a website template and I though of a cool new trick to save time when changing large text files. I want to have a separate plain text document that when the page loads, it contents will be transferred to my paragraph.
Example:
The text document:
Introducing the new red glasses.
The paragraph's text will be loaded and be the same as the text documents like so:
<p> Introducing the new red glasses. </p>
So how can I do something like this? Is it even possible? I am willing to use HTML, CSS, jQuery and Javascript.
You could use AJAX:
$.get( "mytext.txt", function( txt ) {
$( "#myParagraph" ).text( txt );
});
You will need to host your HTML from a server, or otherwise it won't work
This question already has answers here:
Custom attributes - Yea or nay?
(15 answers)
Closed 9 years ago.
I am working on a website using HTML5. I have a jQuery script that shows a custom tooltip on all elements that have the title attribute. I also have a script that makes an alert message appear when the user clicks a picture. The alert message will say what the title attribute equals. Unfortunately the tooltip script and the alert script interfere with each other.
My question is:
Can I make up an attribute?
Not exactly, but HTML 5 provides data-*.
In html5 , use data-XX to produce extra attributes.
see : http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
You can make an aditional attribute, just by naming it.
<img src="abc.jpg" data-opens="abc" data-test="abc" id="image" />
And access it in jQuery by typing
$("#image").attr("data-opens")..
Like:
alert($("#image").attr("data-opens") + " " + $("#image").attr("data-test"));
Edited, thanks to GCyrillus.
Specifically answering you question: you can make up attributes.
But your HTML will no longer be valid and since you are adding them just to store information, you should use de data-* form to add you own data storage for an element, plus it will still be a valid HTML