473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Get text within tags or get class instead of Id

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?

Nov 2 '05 #1
3 6869
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
Nov 3 '05 #2
Thanks a lot. It really helped me. Now I can get the contents of the
article with el.innerText.

Nov 4 '05 #3
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.

Nov 4 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Mc Mahon | last post by:
Hi all, I currently use a class to interface with my databases, allowing me to insert, update, delete and retrieve records from the database as methods of the class. I have now created a...
2
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone...
4
by: Christopher | last post by:
I am trying to load a UserControl which inherits from one of our customer classes which in turn inherits from UserControl. We use Page.LoadControl() from within our ASP.NET pages to load these...
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
4
by: ThunderMusic | last post by:
Hi I want to do just like in the subject ... to have some text and tags between the start and end tags of my user-control. Is there a simple way of doing that? for instance, between the tags,...
5
by: Piotrekk | last post by:
Hi Having a keyword i need to search HTML file for keyword dismissing all the tags, and checking only plain text. Is there an easy way to do it in C#? Thanks PK
5
by: hurricane_number_one | last post by:
I'm trying to have a class, which uses threads be able to raise events to the form that created it. I've seen solutions around the net for this, but I didn't really like their implementation. ...
0
by: Linda Liu[MSFT] | last post by:
Hi CES, Thank you for posting here! I notice that you post a same issue in the microsoft.public.dotnet.framework.windowsforms.controls newsgroup and I have replied to that thread. For your...
8
by: hoopy | last post by:
Hi, Im just learning OO in PHP and not sure the best method to use the functions for example in a database wrapper class within another class for example which handles all user authentication. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.