Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

javascript - Find the click ID of a certain button in jquery

For my work I have to claim the "leads" on my company's portal that meets certain criteria. A snippet of the portal is shown below.

enter image description here

I like to write a code that automatically claims the leads that meet those requirements (for instance let's say the lead source is in this case *Too Lazy To Ask) as soon as it becomes available.

Right now, I have written a code using jQuery library (run through Tampermonkey extension) that refreshes the portal at certain intervals and sends a notification as soon as the text *Too Lazy To Ask is found on the page but I have to go and click on the Claim button myself. But now I like to make it better and have it automatically click on the Claim button for me.

Considering that I am a complete beginner in this word, I would appreciate some guidance on how to tackle this problem.

I have also copied a section of the source code for the company's portal below:

<div class="standard">
  <div>
    <h2 id="ctl00_ctl00_main_main_viewingHeader">Lead Bucket</h2>
  </div>        
  <span id="ctl00_ctl00_main_main_pager1">
    <table cellpadding="5px" class="pagerTable">
      <tr class="pager">
        <td class="previous"></td>
        <td class="next"></td>
        <td class="count">
          <span>1-1 of 1</span>
        </td>
      </tr>
    </table>
  </span>
  <span id="ctl00_ctl00_main_main_lblMessage" style="color:Black;font-weight:normal;font-style:italic;">Page refreshes every 2 mins.</span>
  <table class="grid borderTop" cellpadding="0px" cellspacing="1px">
    <tr class="listItem1">
      <div>
        <table class="listItem1" cellpadding="0px" cellspacing="0px">
          <tr>
            <td>
              <a href="ViewLead.aspx?isLead=true&AutoLeadID=981039530" alt="View Lead">Dealer Test Test2</a>
            </td>
            <td><label>2/4/21</label></td>
            <td class="green">0 Hrs Old</td>
          </tr>
          <tr>
            <td colspan="2"></td>
            <td>*Too Lazy To Ask </td>
          </tr>
          <tr>
            <td>
              <input type="submit" name="ctl00$ctl00$main$main$rpt$ctl01$btnClaim" value="Claim" id="ctl00_ctl00_main_main_rpt_ctl01_btnClaim" />
            </td>
            <td colspan="2">Iddm Leads</td>
          </tr>
        </table>
      </div>
    </tr>
  </table>
  <span id="ctl00_ctl00_main_main_pager2">
    <table cellpadding="5px" class="pagerTable">
      <tr class="pager">
        <td class="previous"></td>
        <td class="next"></td>
        <td class="count">
          <span>1-1 of 1</span>
        </td>
      </tr>
    </table>
  </span>
  <span id="ctl00_ctl00_main_main_Timer1" style="visibility:hidden;display:none;"></span>
</div>
<div style="text-align: right;">
  <a href="../../LogOut.aspx" id="ctl00_ctl00_aLogOut">Log Out</a>
</div>
<script type="text/javascript">
//<![CDATA[
Sys.Application.add_init(function() {
  $create(Sys.UI._Timer, {"enabled":true,"interval":120000,"uniqueID":"ctl00$ctl00$main$main$Timer1"}, null, null, $get("ctl00_ctl00_main_main_Timer1"));
});
//]]>
</script>
question from:https://stackoverflow.com/questions/66054736/find-the-click-id-of-a-certain-button-in-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The 2 criteria that makes THAT button are elem.tagName=="INPUT" && elem.type=="submit"

I don't know what element you access when you know a "Claim" has come in, but once a direct or indirect child of this element is the click button, this function would work

var elems=document.getElementsByClassName('listItem1')
//the same method can be used to find the element that has any certain properties(like elem.innerHTML being something.. or elem.src being something)

function elemSearch(elem,fn){
  function recurse(elem){
    [...elem.children].forEach(a=>{
      fn(a); recurse(a)
    })
  }
  fn(elem); recurse(elem)
}

[...elems].forEach((a,i)=>{
  elemSearch(
    elems[i],
    (elem)=>{
      if(elem.tagName=="INPUT"&&elem.type=="submit"){console.log(elem)}
    }
  )
})
<body><div class="standard">
        <div>
            <h2 id="ctl00_ctl00_main_main_viewingHeader">Lead Bucket</h2>
        </div>        
        <span id="ctl00_ctl00_main_main_pager1"><table cellpadding="5px" class="pagerTable"><tbody><tr class="pager"><td class="previous"></td><td class="next"></td><td class="count"><span>1-1 of 1</span></td></tr></tbody></table></span>
        <span id="ctl00_ctl00_main_main_lblMessage" style="color:Black;font-weight:normal;font-style:italic;">Page refreshes every 2 mins.</span>
        
                    <div>
                        </div><table class="grid borderTop" cellpadding="0px" cellspacing="1px">
                
                <tbody><tr class="listItem1">
                    </tr></tbody></table><table class="listItem1" cellpadding="0px" cellspacing="0px">
                            <tbody><tr>
                                <td>
                                    <a href="ViewLead.aspx?isLead=true&amp;AutoLeadID=981039530" alt="View Lead">
                                        Dealer Test Test2
                                    </a>
                                </td>
                                <td><label>2/4/21</label></td>
                                <td class="green">0 Hrs Old</td>
                            </tr>
                            <tr>
                                <td colspan="2"></td>
                                <td>*Too Lazy To Ask </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="submit" name="ctl00$ctl00$main$main$rpt$ctl01$btnClaim" value="Claim" id="ctl00_ctl00_main_main_rpt_ctl01_btnClaim">
                                </td>
                                <td colspan="2">Iddm Leads</td>
                            </tr>
                        </tbody></table>
                    </div>
                
            
                
                    
        <span id="ctl00_ctl00_main_main_pager2"><table cellpadding="5px" class="pagerTable"><tbody><tr class="pager"><td class="previous"></td><td class="next"></td><td class="count"><span>1-1 of 1</span></td></tr></tbody></table></span>
        <span id="ctl00_ctl00_main_main_Timer1" style="visibility:hidden;display:none;"></span>
    


    <div style="text-align: right;">
        <a href="../../LogOut.aspx" id="ctl00_ctl00_aLogOut">Log Out</a></div>
    
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...