Search code examples
jquerycssreplaceparent

How to replace .click with auto function


This function will replace parent .wmessage div css with .v-boxinner div css.

The problem now is that this function start on click. What I need is to replace click function with avto start.

$(".wmessage").parent().click(function(){
    var array = ['width'];
    var $this = $(this);
    $.each( array , function(item, value) {
        $(".v-boxinner").css(value, $this.css(value));
    });
});

This is what I was looking for:

$(document).ready(function(){
var properties = ["width"];
var src = $(".wmessage").parent();
$.each( properties , function(item, value) {
    $(".v-boxinner").css(value, src.css(value));
});

});

Thank you @BeNdErR


Solution

  • If you want to run the function automatically when the page loads you have to call it once the document is ready:

    $(document).ready(function(){
        //--- do what you need here
    });
    

    Here's an example, edit it to suit your needs

    $(document).ready(function(){
        var properties = ["background", "width"];
        var src = $("#copyme");
        $.each( properties , function(item, value) {
            $("#target").css(value, src.css(value));
        });
    });
    

    fiddle here: http://jsfiddle.net/s00kpL1b/