GetElementsByName: manage the last partial name

I can get div elements by id and only use the "first" partial name

Html

<div id="first.1.end">first.1.end</div>
<div id="first.2.end">first.2.end</div>
<div id="two.3.end">two.3.end</div>
<div id="first.4.end">first.4.end</div>

      

Js

function getElementsByIdStartsWith(selectorTag, prefix) {
    var items = [];
    var myPosts = document.getElementsByTagName(selectorTag);
    for (var i = 0; i < myPosts.length; i++) {
        if (myPosts[i].id.lastIndexOf(prefix, 0) === 0) {
            items.push(myPosts[i]);
        }
    }
    return items;
}
var postedOnes = getElementsByIdStartsWith("div", "first");
alert(postedOnes.length);

      

It counts 3 divs (warning).

But how can I use the final name for the search? For example, using "end"?

+3


source to share


3 answers


From MDN Attribute Selector :

[attr ^ = value] Represents an element with an attribute name attr and a value prefixed with value.

[attr $ = value] Represents an element whose attribute name is attr and whose value is suffixed with "value".

Thus, you can use [id^="first"]

start with to find elements with id "first"

. and use [id$="end"]

to find items with "end"

.

how

// This find all div which id ends with "end".
var divs = document.querySelectorAll('div[id$="end"]');

      



or use jQuery:

$('div[id$="end"]');

      

Alternatively, you can combine multiple attribute selectors to find a more specific element:

// As we only use querySelector, it find the first div with id starts with "two" and ends with "end".
var divStartAndEnd = document.querySelector('div[id^="two"][id$="end"]');

      

See demo on jsfiddle

+3


source


I guess such a choice might be possible with jQuery + regex. Look at this

How can I select an element by ID using jQuery using regex?



Possibly something on the line you want.

+1


source


Here I am allowing the user to pass all three parameters. suppose the user has not passed midmatch, so it will only return the match of the first and last.

Below is the working code:

It will return 1 invoice:

function getElementsByIdStartsWith(selectorTag, firstmatch, midmatch, lastmatch) {
    var items = [];
    var myPosts = document.getElementsByTagName(selectorTag);
    for (var i = 0; i < myPosts.length; i++) {
        var firstmatchIndex = firstmatch?myPosts[i].id.indexOf(firstmatch)>-1?true : false : true;
        var midmatchIndex = midmatch?myPosts[i].id.indexOf(midmatch)>-1?true : false : true;
        var lastmatchIndex = lastmatch?myPosts[i].id.indexOf(lastmatch)>-1?true : false : true;
        if (firstmatchIndex && midmatchIndex && lastmatchIndex  ) {
            items.push(myPosts[i]);
        }
    }
    return items;
}
var postedOnes = getElementsByIdStartsWith("div", "first", "2", "end");
alert(postedOnes.length); // now it will show only one in alert.

      

It will return 3 invoices:

function getElementsByIdStartsWith(selectorTag, firstmatch, midmatch, lastmatch) {
    var items = [];
    var myPosts = document.getElementsByTagName(selectorTag);
    for (var i = 0; i < myPosts.length; i++) {
        var firstmatchIndex = firstmatch?myPosts[i].id.indexOf(firstmatch)>-1?true : false : true;
        var midmatchIndex = midmatch?myPosts[i].id.indexOf(midmatch)>-1?true : false : true;
        var lastmatchIndex = lastmatch?myPosts[i].id.indexOf(lastmatch)>-1?true : false : true;
        if (firstmatchIndex && midmatchIndex && lastmatchIndex  ) {
            items.push(myPosts[i]);
        }
    }
    return items;
}
var postedOnes = getElementsByIdStartsWith("div", "first", "", "end");
alert(postedOnes.length); // now it will show only three in alert.

      

if you don't want to consider any parameter, just pass an empty string ( "" ) when calling the function.

Hope this helps you :)

+1


source







All Articles