Friday, August 23, 2013

How can i add css with !important , only using jquery

jQuery code


var div = $(".my_class")
div.click(function() {
 div.style("color", "blue", "important")
        div.append("<div>Color: " + div.style('color') + "</div>")
 div.append("<div>Is_Important: " + div.style().getPropertyPriority('color') + "</div>")
})

$.fn.style = function(styleName, value, priority) {
    var node = this.get(0)
    if (typeof node == 'undefined') {
        return
    }
    var style = node.style
    if (typeof styleName != 'undefined') {
        if (typeof value != 'undefined') {
            var priority = typeof priority != 'undefined' ? priority : ''
            style.setProperty(styleName, value, priority)
        }
        else {
            return style.getPropertyValue(styleName)
        }
    } 
    else {
        return style
    }
}

And finally jsFiddle link

Click here to view effect

No comments:

Post a Comment