0

I made a div with jquery, and after that div is made i want to change the css of it. However, the change doesn't work.

This is my HTML:

<form>
        <input type="text" id="color" placeholder="color"><br>
        <input type="text" id="height" placeholder="height"><br>
        <input type="text" id="width" placeholder="width"><br>
        <input type="button" id="make" value="Make">
    </form>

and this is the jquery:

$(document).ready(function() {
            $('#make').click(function() {
                if (($('#color').val().length) == 0 || ($('#height').val().length) == 0 || ($('#width').val().length) == 0 ) {
                    alert('lol')
                } else {
                    var color = $('#color').val();
                    var height = $('#width').val();
                    var width = $('#height').val();
                    $('body').append('<div id="kast" style="background:'+color+'; width:'+width+'px; height:'+height+'px;"></div>')
                    $('#make').remove();
                    $('form').append('<input type="button" id="change" value="Change">');

                }
            })
            $('#change').click(function() {

                var color = $('#color').val();
                var height = $('#width').val();
                var width = $('#height').val();
                $('#kast').css('background-color', color);
            })
        })

I hope someone can help me :)

1
  • The element is not in the code when the page loads. you first have to enter the color and the dimensions. then you can click on make. After you've made the div #kast then the button change appears Commented Nov 21, 2013 at 12:19

4 Answers 4

1

Try 'on' as you are creating it dynamically : on

 $('body').on('click','#change',function() {
       var color = $('#color').val();
       var height = $('#width').val();
       var width = $('#height').val();
       $('#kast').css('background-color', color);
    })
Sign up to request clarification or add additional context in comments.

Comments

1

As you need to use .on() for dynamic elements

$('form').on('click', '#change', function() {
    var color = $('#color').val();
    var height = $('#width').val();
    var width = $('#height').val();
    $('#kast').css('background-color', color);
})

DEMO

Comments

1

Replace click() with event delegate:

$(document).on('click, '#change', function() {
    ...
});

Comments

1

in this situation that you can't just use as $('#change').click to bind a event on a DOM object which you created after the page was already loaded. You should use these below:

$(document).on('click', '#change', function() {

          var color = $('#color').val();
          var height = $('#width').val();
          var width = $('#height').val();
          $('#kast').css('background-color', color);

      }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.