Jump to content

Recommended Posts

I want to get the value of an element (a span in this case), and display it on my page. Told to do this in javascript, all I have come up with is

var target = '';
target.setAttribute( "src", "urlhere" ); 
target = document.getElementsByTagName( "span" )[3].value; 

 

It obviously doesn't work. The element doesn't actually have an ID, just it's class is unique. So how would I call the text from just the element on the page? I don't know where to start.

 

Thanks...

Link to comment
https://forums.phpfreaks.com/topic/145016-get-text-from-element-on-external-page/
Share on other sites

finding elements by className is not natively supported in all major browsers :-X but I have used this javascript function before for what you are needing to do.

 

function getElementsByClassName(className, tag, elm){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";
var elm = elm || document;
var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i<length; i++){
	current = elements[i];
	if(testClass.test(current.className)){
		returnElements.push(current);
	}
}
return returnElements;
}

 

----->http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

 

Also, I've been using this that I found on the internet

var allHTMLTags = new Array();
function getElementByClass(theClass) {
//Create Array of All HTML Tags
var allHTMLTags=document.getElementsByTagName(”*”);
//Loop through all tags using a for loop
for (i=0; i<allHTMLTags.length; i++) {
//Get all tags with the specified class name.
if (allHTMLTags[i].className==theClass) {
//Place any code you want to apply to all
//pages with the class specified.
//In this example is to “display:none;” them
//Making them all dissapear on the page.
allHTMLTags[i].style.display=’none’;
}
}
}

 

 

Hmmm....yes, but I also need to be able to do this function on an external page

Depends if that external page is on a different domain javascript can't be cross domain. Even if it is on the same server PHP is probably better to use. You can use cURL or just parse the page to a string. Then you could do a regex to fetch the element you want. Or use DOM

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.