§
Napisano: 16-06-2025
Edytowano: 16-06-2025

Hi,

I have this kind of html access somes users : USER-1 => </ a href="javascript:void(0);" class="fsb-button green" onclick="jQuery.ajax({'url':'works/search','data':{'YII_CSRF_TOKEN':'90969e6b0f39bb978c5ac0dba71b4c3f33069108','user':'285'},'dataType':'json','success':fsb.ajax.callback,'cache':false});return false;">Assign Work USER-2 => </ a href="javascript:void(0);" class="fsb-button green" onclick="jQuery.ajax({'url':'works/search','data':{'YII_CSRF_TOKEN':'90969e6b0f39bb978c5ac0dba71b4c3f33069108','user':'25964'},'dataType':'json','success':fsb.ajax.callback,'cache':false});return false;">Assign Work

Only "user" change.

I would like create a function to assign a different action between eachone.

I think i need have something like "if user different 285 do that, if user is 285 do this"

I understand to access something ByClassName, ByID, but not see how i can do in this case. Best for help.

§
Napisano: 30-06-2025
Edytowano: 30-06-2025

You can try with queryselector, or queryselectorall, write your css there.

let element = document.querySelector(`a.fsb-button.green[onclick*="'user':'285'"]`);
element.setAttribute("onclick", "do_something()";
element.style.background="rgba(111,111,111,0.15)";

Instead of setting the attribute onclick, you might want to create a click event listener and check it that way, just make sure to stop any bubbling.

element.addEventListener("click", (e)=>{
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
    // do_something...
},true)
§
Napisano: 02-07-2025

Thank you for reply.

the idea is find "user:285" on the current page and open his page on new tab if it's founding.
If not, script can click another button. I have already this script for that :
function autoClick(){
// Button WORK
if(document.getElementsByClassName("fsb-button fsb-button-double-height w80p").length>0){
document.getElementsByClassName("fsb-button fsb-button-double-height w80p")[0].click();
}
// ?? setTimeout(5000);
//TYPE OF WORK
document.getElementById('FormWorks_plan').value = 'shortest'
// SENDING WORK
if(document.getElementsByName('yt0').length>0){
document.getElementsByName('yt0')[0].click();
}

Actually, I try to work on your reply to open an alert box if it's find user:285 but no works :(

§
Napisano: 02-07-2025
Edytowano: 02-07-2025

Does that button open what you want in new tab when clicked? If so, just do this:

let element = document.querySelector(`a.fsb-button.green[onclick*="'user':'285'"]`); //find the element, if it doesn't work modify the selector
element.click(); //click it

You can check to see if the selector finds what you are looking for with the inspector (ctrl + shift + c)

§
Napisano: 02-07-2025

Try this:

let elements = document.querySelectorAll("a.fsb-button.green[onclick]");
elements.forEach((e,i)=>{
    let user = e.getAttribute("onclick").match(/'user'\s*:\s*'(\d+)'/);
    if(user){
        let id = user[1];

        if (id == '285') {
            console.log("user 285 found");
            e.click(); //click the button
        }

    }
});

This uses regex to find the user id after grabbing all buttons. Just add more checks for each id you want to do something with.

Odpowiedz

Zaloguj się, by odpowiedzieć.