Back to Basics

Posted in: javascript

An Example

function hasClass ( domElement , className ) { //code here... } function removeClass ( domElement , className ) { //code here... }

The Answers

function hasClass ( domElement , className ) { return ( ' ' + domElement . className + ' ' ) . indexOf ( ' ' + className + ' ' ) >= 0 ; }

function removeClass ( domElement , className ) { domElement . className = domElement . className . replace ( new RegExp ( '(^|\\s)' + className + '(?:\\s|$)' ), '$1' ); }

var className = 'myclassname' ; domElement . className = 'myclassname yeah' ; removeClass ( domElement , className );

'(^|\\s)myclassname(?:\\s|$)'

'(^|\\s)myclassname(\\s|$)'

domElement . className == className //means '^className$' || domElement . className . startsWith ( className + ' ' ) //means '^className\\s' || domElement . className . endsWith ( ' ' + className ) //means '\\sclassName$'

Conclusion

Please enable JavaScript to view the comments powered by Disqus.

Disqus