I am trying to get an array of objects belonging to a certain class on a webpage.
This is my very first experiment with JQuery and JS, so please be understanding.
I would like to return an array of all items belonging to the class dataRow odd.
Currently I am trying $('.dataRow odd') and $('.dataRow.odd') as seen here. and here.
For some of the outer classes, this method seems to work, and I can get the element. As soon as I get into one that is further in (more indented), it won't.
This is the html of the table that I'm trying to access:
<div class="listRelatedObject caseBlock">
<div class="bPageBlock brandSecondaryBrd secondaryPalette">
<form action="/p/case/CaseMassAction?retURL=%2F500%3Fisdtp%3Dlt%26fcf%3D00BE0000003Suuk&ent=Case&from=&isdtp=lt" id="actionForm" method="POST" name="actionForm" onsubmit="return verifyChecked(actionForm, 'ids', 'Please select at least one row')" target="mainFrame">
<input type="hidden" name="isdtp" id="isdtp" value="mn">
<input type="hidden" name="retURL" id="retURL" value="/500?isdtp=lt&fcf=00BE0000003Suuk">
<div class="pbHeader">
<div class="listHeader">...</div>
</div>
<div class="pbBody">
<table class="list" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="headerRow">...</tr>
<!-- ListRow -->
<tr class="dataRow even first bRowHilight" id="row_500E000000B8LgY" onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onmouseover="if (window.hiOn){hiOn(this);}">...</tr>
<!-- ListRow -->
<tr class="dataRow odd" id="row_500E000000B77FP" onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onmouseover="if (window.hiOn){hiOn(this);}">...</tr>
<!-- ListRow -->
<tr class="dataRow even" id="row_500E000000B8NHk" onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onmouseover="if (window.hiOn){hiOn(this);}">...</tr>
<!-- ListRow -->
<tr class="dataRow odd last" id="row_500E000000B7TIG" onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onmouseover="if (window.hiOn){hiOn(this);}">...</tr>
</tbody>
</table>
</div>
</form>
</div>
Here, when I type #('.className') in the console I get "null" out.
However, when I type in, for example,
$('.bodyDiv.brdPalette.brandPrimaryBrd')
I get out the element:
<div class="bodyDiv brdPalette brandPrimaryBrd">...</div>
(This div is not included in the source above, but it is one of the first (least indented) divs on the page.
I have also tried getElementsByClassName(".dataRow.odd") and getElementsByClassName(".dataRow odd") but neither of these work.
Is there any reason that the $('.myClass') selector should not work?
My goal is to write a script that will run and tell me how many rows exist in the table. I have this so far:
// ==UserScript==
// @name SalesForce JS to AutoRefresh
// @version 0.1
// @match https://na9.salesforce.com/ui/desktop/DesktopPage
// @copyright 2012+, You
// ==/UserScript==
var time = new Date().getTime();
var numCases = $('.dataRow.odd').length + $('.dataRow.even').length;
function refresh()
{
console.log("Inside Function");
if(new Date().getTime() - time >= 10000)
{
time = new Date().getTime();
freshImage.click();
console.log("Refreshed");
console.log("num Cases = " + numCases);
if($('.dataRow.odd').length + $('.dataRow.even').length > numCases)
{
while(numCases-- > $('.dataRow.odd').length + $('.dataRow.even').length)
beep(100,0);
}
}
setTimeout(refresh, 1000);
}
var beep = (function () { .... /*beeps*/})();
setTimeout(refresh, 1000);
I think everything is working, except that $('.dataRow.odd').length + $('.dataRow.even').length will not work (because $('dataRow.odd') returns null)
Thanks for the responses so far. Sorry if my answer wasn't clear before--still pretty new to this.
Thanks!
$('.dataRow .odd')document.querySelectorAll(".dataRow.odd")ordocument.getElementsByClassName("dataRow odd"). In jQuery, use$('.dataRow.odd')$('.dataRow.odd')should do what you want. Can you elaborate on this: "For some of the parent classes, this method seems to work, and I can get the element. As soon as I get into an ancestor it will not work."document.getElementsByClassName("dataRow odd"). That is the correct syntax;getElementsByClassNameonly has one parameter. (MDN)