View Full Version : Check if an elements value or innerHTML or a predefined string contains certain text
uncrocks
10-22-2008, 11:20 PM
Use his Code...
Its better...
document.ifElementContains= function ifElementContains(eid, value){
var vapp= (eid.charCodeAt ? document.getElementById(eid) : eid ) || eid;
var val = vapp.value || vapp.innerHTML || window[vapp] || vapp;
return !! val.match(value);
}
Tag's innerHTML, Input field's value, and strings:
document.ifElementContains("name_of_sting_or_string_or_element_ID_or_elementObject", "text_to_be_searched");
rnd me
10-23-2008, 01:02 AM
sorry to be a bummer/jerk/naysayer/know-it-all but:
eval is bad, your code uses internal globals and is needlessly complicated, proprietary, and fragile.
document.ifElementContains= function ifElementContains(eid, value){
var vapp= (eid.charCodeAt ? document.getElementById(eid) : eid ) || eid;
var val = vapp.value || vapp.innerHTML || window[vapp] || vapp;
return !! val.match(value);
}
this does the same thing without the eval or extra "type" argument.
you can also pass either a string to search or the name of the string's var.
i don't like if statements either (they usually double your coding), so my version avoids them.
Tag's innerHTML, Input field's value, and strings:
document.ifElementContains("name_of_sting_or_string_or_element_ID_or_elementObject", "text_to_be_searched");
sorry. i was bored and i like improving things.
uncrocks
10-23-2008, 03:32 AM
var vapp= (eid.charCodeAt ? document.getElementById(eid) : eid ) || eid;
Thanks, i am kinda new to javascript, by the way, what do the question marks, colons, and double bar lines do?
As much as i've tried, i just can't understand them...
rangana
10-23-2008, 04:13 AM
It's a ternary operation (?:) which is exactly the same as the if else statement.
So this part:
var vapp= (eid.charCodeAt ? document.getElementById(eid) : eid ) || eid;
...could be translated to:
var vapp;
if(eid.charCodeAt)
vapp=document.getElementById(eid) || eid;
else
vapp=eid || eid;
I used to call the double bars an or statement.
For further reading:
http://codingforums.com/showpost.php?p=90154&postcount=3
http://docs.sun.com/source/816-6409-10/expr.htm#1008690
Please refer on rnd me's explanation regarding double bars (in depth).
rnd me
10-23-2008, 06:47 AM
don't mind helping out, i think it's a decent idea.
to clarify your questions where rangana left off:
the double bars are used to re-assign values inline, left - to right, taking the first one that "works".
sometimes called the default operator, double bars can convey multiple possibilities to an assignment (the stuff to the right of an = sign).
picking up mid-function, let's consider an example of a string:
vapp = "hello world"
what val = vapp.value || vapp.innerHTML || window[vapp] || vapp; means is at first glance possibly the same as
val = vapp.value
if it were an input this is the point at which it bails out and the remaining options to the right are never evaluated.
IF vapp.value is "falsy" (which means that the value itself is equal to undefined, false, 0 , NaN, null, etc) then go to the next option to the right; vapp.innerHTML.
remember that strings don't have a .innerHTML so this evaluates to undefined for our example. if it were an element, it would check out here.
continuing, it looks for window[vapp].
if vapp if a string name of a global variable (and that global is not empty) this is where the variable name scenario terminates.
if we made it this far, and we know it was a string, and that it did not meet any of the above conditions, then we must have passed the actual string we wanted to search.
feel free to modify the code and add further possibilities. you could add a .href to grab urls from <A> tags, or substitute "vapp.textContent || vapp.innerText" for "vapp.innerHTML" to grab just the text from an element instead of the HTML. adjust as needed...
now that we have the string to search, we then take the string (vapp or "hello world"), and search it for the sub-string value.
finally, !! turns the truthy-ness of the match operation into an actual boolean, giving us a simple up/down decision about the occurrence of the sub-string.
"||" can replace quite a bit of flow code in some situations.
uncrocks
10-24-2008, 01:25 AM
Thank you both!! :thumbsup:
Trinithis
10-24-2008, 07:35 AM
eid.charCodeAt
? document.getElementById(eid)
: eid
is rather unintuitive.
How about one of the following:
eid.constructor == String
? document.getElementById(eid)
: eid
typeof eid == "string"
? document.getElementById(eid)
: eid
eid instanceof String
? document.getElementById(eid)
: eid
rnd me
10-24-2008, 09:04 AM
eid.charCodeAt
? document.getElementById(eid)
: eid
is rather unintuitive.
well, that's not my code, and it won't work for all the scenarios i outlined.
specifically, if they pass the string they want to search.
if the document.getElementById comes back null, you need to default to the original string.
not sure what you mean by unintuitive.
i use object detection: charCodeAt is a standard property of all strings in javascript. Thus if an object contains the property "charCodeAt", it is a string.
I guess someone could add a .charCodeAt prototype method to HTMLElement, but that's non standard, and about any code can be broken by poorly reusing native method names...
Even given that extreme possibility, one could still pass the Element as an object and the function would perform just fine.
It may not be very common as came up with this type checking method myself, but to me it's very intuitive. I had written a "post a javascript" (easier type handling) a while back about adding a .type property to all the native prototypes. i also was using a .isString, .isArray kinda thing for a while. But that made me use globals prototypes, which i try to avoid. i realized most types already have unique properties, so i could use these instead.
I thought the traditional typeofs and string comparisons were bulky and slow.
if you like the simple notation and fast lookup speed (avoiding a comparison speeds execution), here are a couple examples of how to do type sorting via native object property detection:
String: x.charCodeAt;
Array : x.join;
Function : x.call;
Date: x.getTime;
Element: x.nodeName;
Trinithis
10-24-2008, 10:23 AM
Sorry about the ambigiuty. What I meant is that if one is to check to see if an object is a String (or whatever), why not do so explicitly? When I was reading your code, I had to pause a moment to understand that you were simply testing the object's type.
Not to say that testing types explicity is better than what you are doing though, but for me, I would rather not second guess code.
Perhaps it's just me and that I like rigor ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.