MJ
asked on
Detect if Non-traditional element exists or not using Javascript
ASKER
Unfortunately that Tag name ("oe-i18n-msg") is common.
What about using the max attributes we can like :
if( document.querySelectorAll('oe-i18n-msg.style-scope.session-timeout[msgid]').length ){
console.log( 'Exist' );
}else{
console.log( 'Not Exist' );
}
As has been stated by both Chimay and Zakaria, you can use either pure JavaScript or a library such as jQuery to do this.
jQuery:
Pure JavaScript:
jQuery:
var exists = $("oe-i18n-msg[msgid='1_SessionTimeout']").length > 0;
Pure JavaScript:
var exists = false;
var elements = document.getElementsByTagName("oe-i18n-msg");
for(var i = 0; i < elements.length; i++) {
exists = elements[i].getAttribute('msgid') === '1_SessionTimeout';
if(exists){
break;
}
}
Here's a working fiddle for both approaches.
If the tag is common how do you want to search for it?
There needs to be something we can search for.
You can find it by id like this
By Attribute value
There are many ways to do this but to answer your question we need to know
What you know about the element that is going to be on the page - what unique consistent value will always be there.
Once we know that we can show you the code on how to find that.
There needs to be something we can search for.
You can find it by id like this
By Attribute value
if (document.querySelectorAll("[msgid='1_SessionTimeout']").length) {
console.log('Exists');
}
If you want to find it by class thenif (document.getElementsByClassName('session-timeout').length) {
console.log('Exists');
};
There are many ways to do this but to answer your question we need to know
What you know about the element that is going to be on the page - what unique consistent value will always be there.
Once we know that we can show you the code on how to find that.
ASKER
Only the msgid will contain a unique ID of "1_SessionTimeout".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all!
You are welcome.
Open in new window
PS: Please use [ code ] tag without spaces to post code/content.