Page 1 of 1

how to extract the onclick value from a string

Posted: Tue Jan 25, 2005 11:10 am
by jasongr
Hello

Assuming a string has the value:
onclick="<javascript code here>"
or
onclick='<javascript code here>'

is there an easy method using regular expression to obtain the JavaScript code without resorting to parsing?
onclick is case insensitive and the <javascript code here> may contain double or single quotes, but you can assume it is legal

any help would be appreciated

Posted: Tue Jan 25, 2005 11:17 am
by feyd
time to get the regex book, friend.

Code: Select all

preg_match_all('#<\s*\w+.*?\s+onclick\s*=\s*(&#1111;"'']?)(.*?)(\\1).*?>#si', $text, $matches);
print_r($matches);
code is untested.

Posted: Tue Jan 25, 2005 9:08 pm
by protokol
Or, if you need this through javascript, then use a combination of getElementById() and the object's properties.

Posted: Wed Jan 26, 2005 2:31 am
by jasongr
can I obtain the onclick properties of the object via JavaScript?
that would be very nice to know