/    Sign up×
Community /Pin to ProfileBookmark

Get information from within a span class

I have select words that are within a <span class=”word”>example</span>. I need to make javascript pull the word “example” here so I can use that as a search term, and return the value which is linked to that word in an array. What would be the best way to do this?

I got this far, but now I am stuck…

[CODE]
var arrElements = document.getElementsByTagName(‘span’);
var words = new Array();
for (i = 0; i < arrElements.length; i++)
{
// Save the current element
oElement = arrElements[i];
if(oElement.className == “word”)
{
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jvanamaliOct 10.2006 — Give span an id

[B]<span id="s1">example</span>[/B]

then in javascript function you retrieve the text as

[B]document.getElementById("s1").innerHTML[/B]
Copy linkTweet thisAlerts:
@mynameisdave145authorOct 17.2006 — OK, so I got it to cycle through all spans with this, but the problem is that it prints the contents of any span tag, and I only want the ones with the id="word"
[CODE]
var arrElements = document.getElementsByTagName('span');
for (i=0;i<arrElements.length;i++)
{
var a = arrElements[i].firstChild;
alert (a.nodeValue);
}
[/CODE]


and I got it to print via a specific tag ID like this, which prints the first id="word" value, but I have no idea how to get it to the next. Please help!
[CODE]
alert(document.getElementById('word').firstChild.nodeValue);
[/CODE]
Copy linkTweet thisAlerts:
@semi-sentientOct 17.2006 — ID's need to be unique, meaning that you can only assign id="word" to one element. If you need specific elements as a "word" placeholder, then you can just add a number to "word" and loop through those.

For example, if you have the following span elements:
&lt;span id="word1"&gt;value&lt;/span&gt;&lt;br /&gt;
&lt;span id="word2"&gt;value&lt;/span&gt;&lt;br /&gt;
&lt;span id="blah"&gt;You won't get the text of this span...&lt;/span&gt;


You could loop through those like this:
var arrElements = document.getElementsByTagName('span');

for (var i = 0; i &lt; arrElements.length; i++) {
if (arrElements[i].id == ("word" + i)) {
alert(arrElements[i].firstChild.nodeValue);
}
}
×

Success!

Help @mynameisdave145 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...