Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Get text within tags or get class instead of Id

Reply
Thread Tools

Get text within tags or get class instead of Id

 
 
svintuss
Guest
Posts: n/a
 
      11-02-2005
Hi. I'm trying to create a widget for Tiger, which should use a
web-dictionary. I want the widget to display translation within its
window, so it has to copy all the data within the tags <span
class="lingvo-article"> </span>.
Is there a method like document.getElementById to get the class
contents?
Or it's only possible to search for opening and ending tags, then copy
the text inside?

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-03-2005
svintuss wrote:
> Hi. I'm trying to create a widget for Tiger, which should use a
> web-dictionary. I want the widget to display translation within its
> window, so it has to copy all the data within the tags <span
> class="lingvo-article"> </span>.
> Is there a method like document.getElementById to get the class
> contents?
> Or it's only possible to search for opening and ending tags, then copy
> the text inside?
>


I haven't written any Dashboard widgets, perhaps there's a better forum
for that. There's some good stuff here:

<URL:http://developer.apple.com/macosx/dashboard.html>

It seems to be pretty much the same as typical web stuff, so you should
be able to get the following working.

There is no 'getElementsByClassName' method, though it is asked for
reasonably often. You have to sift through the document looking for
matches. If you are only looking for span elements, then:


var docBody = document.body || document.documentElement;
if (!docBody || !docBody.getElementsByTagName) return;

var el;
var spans = docBody.getElementsByTagName('span');
var classSpan = [];
var classMatch = /\blingvo-article\b/;

for (var i=0, len=spans.length; i<len; ++i){
el = spans[i];

if (el.className && classMatch.test(el.className)){
// el is a 'lingvo-article' span,
// do something with it
}
}

If you are looking for all elements and not just spans, you'll need to
replace:

var spans = docBody.getElementsByTagName('span');


with something like:

var allElems = document.getElementsByTagName("*");

Presumably this will only ever run on Mac OS X 10.4 or higher, so
feature detection and fall-back is not really required.

I've recently posted stuff on getting the text content of a cell, search
the archive for 'textContent innerText' and sort by date. If Tiger
supports textContent (it's in DOM 3 so maybe not but Firefox has it),
you can get the element's text content quite simply:

var theContent = el.textContent;



--
Rob
 
Reply With Quote
 
 
 
 
svintuss
Guest
Posts: n/a
 
      11-04-2005
Thanks a lot. It really helped me. Now I can get the contents of the
article with el.innerText.

 
Reply With Quote
 
svintuss
Guest
Posts: n/a
 
      11-04-2005
I'm now curious about how to make the function with the text above
process the input, obtained via XMLHttpRequest():

function getArticle(event)
{
var url = "http://lingvo.yandex.ru/en?text=test";
req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
response = req.responseXML; //or req.responseText;
processHTML(response);
}

function processHTML(input)
{
alert(input); //if used responseText it returns raw text of the
page, if responseXML - returns "undefined"
var docBody = document.body || document.documentElement;
if (!docBody || !docBody.getElementsByTagName) return;

var el;
var spans = docBody.getElementsByTagName('span');
var classSpan = [];
var classMatch = /\blingvo-article\b/;
var text = '';

for (var i=0, len=spans.length; i<len; ++i){
el = spans[i];

if (el.className && classMatch.test(el.className)){
text += el.innerText;
}
}

alert(text); //returns nothing in both cases

}

Seems to me, that the processHTML() function works with my page, not
with the one downloaded from the net.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databindings and XML - Binding Text between XML Tags instead of XMLTag Atrribute Information theintrepidfox ASP .Net 0 11-26-2007 12:55 PM
html tags within meta tags allowed? Donald Firesmith XML 5 01-08-2005 11:29 PM
Unable to have tags within tags?? Kwasi Java 13 12-01-2004 02:57 PM
table-tags within div-tags Chris HTML 5 02-10-2004 08:57 PM
Custom Tags within Custom Tags. Ranganath Java 2 10-21-2003 06:14 AM



Advertisments