Jump to content

Find The Tag By "itemprop" Value


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 post
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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites
  • 8 years later...

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...