Search code examples
jquerydojopartial

Dojo equivalent to jQuery's find element by partial ID?


Hey all I am trying to figure out how to go about using Dojo and finding an element by its ID. Reason why I need to find the ID by partial is because it has random number/letters within the ID that change every time the page is loaded.

However, even though it has a random made number/letters on load, it still has a constant ID at the end of it that I define myself:

Example:

<span name="DF98_sj30222_DcIOws34" id="sj346F_46fmsS_54_sERCx_mydefinedid" />

I know that in jQuery I can do the following to find that element:

$('[id*="mydefinedid"]')

But I am unsure of what it would be in Dojo.

if (dojo.query("input[id*='FName']") === "bob") {
   alert('yes');
}

Solution

  • I'm recommend you to use dojo/query with load AMD, Look at here documentation or reference guide here about dojo/query.
    e.g for you.

    //E[id*="FName"] an E(an element of type E) element whose "id" attribute value contains the substring "FName"
    
    require(["dojo/query"], function(query){
                                       //Set color red for example.
            query("input[id*='FName']").style("color", "red");
    });