Thursday, December 27, 2012

Assign class or function to Dom element on the fly

Hello Guys,
        Want to assign the class to dom element on the fly? then simply use javascript function which listed below.

demo.html

Lets assume we have anchor tag on it's click we have to apply css class.

<a id="anchor" href="" target='_blank' class='active' onclick="javascript:open_url(this.id, 'google.com');"> Open this link in new window or tab </a>

// javascript  function
<script type='javascript'>
  function open_url(id, url){
   // verify dom has 'active' class or not
   if ($('#' + id).hasClass('active')) {
     if(url != ''){
    // it's open url in new window
     window.open(url);
   }
  }
}
</script>

No comments:

Post a Comment