Jump to content

Find The Tag By "itemprop" Value


khadem1386

Recommended Posts

HiI want find the tag by "Itemprop" value by java-script.

<td valign="top" itemprop="productID">260871544234</td>

I want a code that it returen the value of a TD that it has an attribute "itemprop='productID'" this code search for <TD> tags and if it has "itemprop='productID'" , get its value, for example it return 260871544234 I want get text that write on <TD></TD> tag. and <TD> Tag must this attribute "itemprop='productID'" so this <TD> is valid to getting it's value. I need 260871544234 thanks for your help

Edited by khadem1386
  • Like 1
Link to comment
Share on other sites

Are you referring to something like this:

<!doctype html><html><head><title>TD</title><script type="application/javascript">window.onload = function init(){var td = document.getElementsByTagName("td");for(var i=0; i<td.length; i++){   if(td[i].id == "productID")  {   alert(td[i].innerHTML);   }}} </script></head><body><table><tr><td id="productID">260871544234</td></tr><tr><td>Hello world!</td></tr></table></body></html>

If so, you just make it more simple by just using document.getElementById;

Link to comment
Share on other sites

if you want to find that custom attribute, you might want to consider jquerys each() and attr()

<script  type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" ></script><script type="text/javascript">/*<![CDATA[*//*---->*/$(document).ready(function(){$("td").each(function(){  if($(this).attr("itemprop")=="productID") { alert($(this).html()) } });});/*--*//*]]>*/</script>  

Link to comment
Share on other sites

Are you referring to something like this:
<!doctype html><html><head><title>TD</title><script type="application/javascript">window.onload = function init(){ var td = document.getElementsByTagName("td"); for(var i=0; i<td.length; i++){   if(td[i].id == "productID")  {   alert(td[i].innerHTML);   }  }} </script></head><body><table><tr><td id="productID">260871544234</td></tr><tr><td>Hello world!</td></tr></table></body></html>

If so, you just make it more simple by just using document.getElementById;

Don E thanks but I can not Use ID attribute. I can use only "Itemprop" what about
if(td[i].Itemprop == "productID") ?????? and <td Itemprop="productID">260871544234</td>

Edited by khadem1386
Link to comment
Share on other sites

if you want to find that custom attribute, you might want to consider jquerys each() and attr()
<script  type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" ></script><script type="text/javascript">/*<![CDATA[*//*---->*/$(document).ready(function(){$("td").each(function(){  if($(this).attr("itemprop")=="productID"){alert($(this).html())}}); }); /*--*//*]]>*/</script>  

dsonesuk thanks you Jquery is so powerful !!!But I can not use any included script on my code (like jquery) in this project :glare: Is there any way to use jquery ,(without including!!!)? Is there any way (code suggestion) to use the clear JavaScript Edited by khadem1386
Link to comment
Share on other sites

I made this function some time ago, I doubt it's very efficient but it should work:

function getElementsByAttribute(Attribute, Value) {var Elements = []; var scanElementNodes = function(Element) {var Nodes = Element.childNodes;var i, j = Nodes.length, Node;var i2, j2, Attributes, Attribute2; for (i=0; i<j; i++) {Node = Element.childNodes[i];if (Node.nodeType == 1) { Attributes = Node.attributes;j2 = Attributes.length;for (i2=0; i2<j2; i2++) {Attribute2 = Attributes[i2];if (Attribute2.nodeName == Attribute && Attribute2.nodeValue == Value) {Elements.push(Node);break;}} scanElementNodes(Node);}}} scanElementNodes(this); return Elements;}HTMLElement.prototype.getElementsByAttribute = getElementsByAttribute;document.getElementsByAttribute = getElementsByAttribute;

then you could do like:

var tds = document.getElementsByAttribute('itemprop', 'productID');alert( tds[0].innerHTML );

Link to comment
Share on other sites

Or just use normal js of jquery formula

window.onload = function(){var tdChild = document.getElementsByTagName("td");for(var i=0; i<tdChild.length; i++){   if(tdChild[i].getAttribute("itemprop")=="productID")  {   alert(tdChild[i].innerHTML);   }}}

Edited by dsonesuk
Link to comment
Share on other sites

  • 8 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...