khadem1386 Posted October 15, 2011 Share Posted October 15, 2011 (edited) 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 October 16, 2011 by khadem1386 1 Link to comment Share on other sites More sharing options...
khadem1386 Posted October 15, 2011 Author Share Posted October 15, 2011 I need a thing such GetElementbyItemprop Link to comment Share on other sites More sharing options...
Don E Posted October 15, 2011 Share Posted October 15, 2011 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 More sharing options...
dsonesuk Posted October 15, 2011 Share Posted October 15, 2011 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 More sharing options...
khadem1386 Posted October 16, 2011 Author Share Posted October 16, 2011 (edited) 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 October 16, 2011 by khadem1386 Link to comment Share on other sites More sharing options...
khadem1386 Posted October 16, 2011 Author Share Posted October 16, 2011 (edited) 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 Is there any way to use jquery ,(without including!!!)? Is there any way (code suggestion) to use the clear JavaScript Edited October 16, 2011 by khadem1386 Link to comment Share on other sites More sharing options...
JamesB Posted October 16, 2011 Share Posted October 16, 2011 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 More sharing options...
dsonesuk Posted October 16, 2011 Share Posted October 16, 2011 (edited) 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 October 16, 2011 by dsonesuk Link to comment Share on other sites More sharing options...
Tobscho Posted September 8, 2020 Share Posted September 8, 2020 I have the same problem I have to read the content = "" in the itemprop, but I can't figure out how to do this. <span itemprop="price" content="399.00">399,00</span> Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now