check for paragraph and inline text - javascript

As title said, i want to check wether the selected text is paragraph or belong to two different paragraph or an inline text,
I am using this java script code for getting text.
var range = window.getSelection().getRangeAt(0);
var selectionContents = range.extractContents();
So need to check for selectionContents.
Thanks in advance.

I suspect this isn't quite a full answer yet so I'll edit and explain further once it's more clear, but is this something like what you're looking for? http://jsfiddle.net/UFdDY/
[ getSelectionHtml() referenced from HTML of selected text ]
EDIT:
Now take a look at the revision, it can tell the difference between inline text and an entire paragraph if this is what you're looking for, but it takes some modification to the html (assumes you're using server side script to generate the output).
http://jsfiddle.net/UFdDY/1/

Related

how to retrieve a line of text from ACE editor?

I integrated ACE editor in my site. I want to retrieve the text in a certain line. I've searched and found This and This. But unfortunately, I can't understand those, as I'm a newbie to the subject. Can anyone give an example for it ?
I got the lines to an array using the following code.
var line = document.getElementsByClassName("ace_line");
Are there any methods like
line[count].getValue
Which can return the string of the text in that line ?
You need to call
line0 = editor.session.getLine(0);
using getElementsByClassName("ace_line") or similar won't work since
ace creates DOM elements only for visible lines.
text content of the DOM element is different from the text in document since tabs and some other characters are replaced by spaces.
line[count] already returns the DOM element of that line. So all you have to do is to extract the plain text from that element. if you are using jQuery you could do:
To get the text of the first line for example (line[0])
var lineText = line[0].text();
If you are not using jQuery and want to do that in javascipt:
var lineText = line[0].innerHTML.replace(/<[^>]*>/g, "");

Changing textContent using javascript, Cushy CMS

OK, I'm going to get a bad rep here for asking too many questions. I have some javascript that dynamically changes content on my page. This works just fine. My issue is that I need to be able to tag all text with 'class="CushyCms"' in order to allow access to the site owner for easy content changes. Here is the basic code for the script, there is more than just the one set but this will give you an idea of what I'm doing. I tried adding the class tag inside the innerHTML, but Cushy couldn't see it.
<script language="javascript" type="text/javascript">
function changeText(idElement) {
if(idElement==0){
document.getElementById('tagmain').innerHTML ='<class="cushycms">Default text to display on page load.';
document.getElementById('tagtext').innerHTML ='<class="cushycms">More default body text on page load.';
}
</script>
I am looking for a way to put these text fields in a hidden div and pull the textContent from there. This is an example of a section that works with Cushy
<h2 class="cushycms">Preventative Maintanence</h2>
I'm beginning to get the hang of javascript, though Java is my primary language. I want to be more rounded i my langauge skills so I am trying to leanr as much as I can. Thanks in advance for the help.
Cushy CMS requires an actual HTML in order to edit. You could use the following:
HTML
<p id="tagmain-replace" class="cushycms" style="display:none;"></p>
<p id="tagmain">Default Text</p>
JS
var newText = document.getElementById('tagmain-replace').innerText;
if( newText != ''){
document.getElementById('tagmain').innerText = newText;
}
I would suggest changing the IDs to better work with your project.

Javascript: get plain text inside a span which has other nested tags present

Just for you guys to note, of course I have read this first:
Javascript get text inside a <span> element
However, my case is not that easy, let alone because I need to do it natively, without jQuery.
Supposing we have this on an arbitrary web page:
<span id="entry1" class="entries">
<img src="http://whereyourpicis.at/pic.jpg" border="0">
++ This is the plain text we want to get from the SPAN block. ++
<span id="nested2"><a onclick="doSomething()">Action!</a></span>
</span>
I've tried anything imaginable, but I can't say any of the "solutions" I tried was a good one, since it feels like a total kludge taking the whole innerHTML and then doing some sed-style regex magic on it.
There must be a more elegant way to accomplish this, which is why I'm asking here.
BTW I've also found out that even nextSibling() cannot work here.
I am not sure this is what you need, because you didn't specify what you need to be an exact output in your example code.
If you need to literally Strip HTML from Text JavaScript
you could use function like this:
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
please check this: http://jsfiddle.net/shershen08/7fFWn/3/
If you want to get only the text nodes within an element, I think you'll need to iterate over the element's childNodes and fetch the text nodes. Here's a quick-and-dirty example of a function that will fetch only the text nodes from a given element (it also skips any text nodes that are just whitespace, since those are often added as a result of HTML formatting but don't really mean anything to a human).

Javascript create element and add HTML

say for instance i have the following line:
var arrowBase = document.createElement('div')
Now within this div tag i want to add some HTML (i.e text).
Then i tried the following:
arrowBase.innerHTML('hello');
However this does nothing:S
i have also tried: arrowBase.HTML('hello');
But once again without any result
I know is that rather simple but in my search i could'nt find the answer hope someone is able to help me out here
Read the docs, it is not a method.
arrowBase.innerHTML = 'hello';
arrowBase.textContent = "HELLO"
also does the same thing but only text can be specified. Whereas in innerHTML html tags can be specified along with the text.

Find certain text and get complete text

I'm using a proxy to scrape data of this url: CNN Article
I would like to get the entire article text (heading not necessarily). So I tried this:
$(data).find("div:contains('Across the river from Cairo')");
This wil find the piece of text but when I do my thing with it myThing = $(this).text(); It seems it is getting a lot more than just the article. This might have something to do with the way the HTML is constructed. If I look at the source I see the article text is confined in p However changing the div:contains in to p:contains only gets me the first few lines (obviously)
So my question is how do I get the article text regardless it's HTML construction. I'm looking for something(code) that will say:
find.('Across the river from Cairo') and get this text and all the text underneath this text();
I'm getting the desired results from that article with the selector p.cnn_storypgraphtxt. To get the whole article, you can use $("p.cnn_storypgraphtxt").text() or
$("p.cnn_storypgraphtxt").map(function(){return $(this).text;}).get().join("\n");
For getting the text that follows a certain expression, you might use .last() to get the last selected node (i.e. the lowermost in the DOM) and then .nextAll() like
$(":contains('Across the river from Cairo')").last().nextAll().text()
but that will contain a lot of unwanted stuff.
Try using
$someString = $(data).find("div:contains('Across the river from Cairo')").html();
use that string for manipulations or whatever.

Categories

Resources