1

Got some errors here. I am trying to use an id which I created using javascript but it won't work.

$('#button').on('click', function() {
    $('#here').html('<p> thank you for clicking </p>');
    $('#here2').html('<input type="button" id="button2" value="click me now" />');
});
$('#button2').on('mouseover' function() {
    $('#button2').css({
        'background-color':'#0FF';
        'width':'400px';
        'height':'200px';
    });
});

I think that it is possible but I don't know how. Please teach me the right way.

gaynorvader
  • 2,553
  • 3
  • 16
  • 31
Jonathan
  • 301
  • 2
  • 13
  • 4
    Possible duplicate of [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Pete Jul 27 '17 at 10:54

1 Answers1

2

Use jquery event validation: Because then DOM was ready, button2 id was not there. So it can be caught like this:

$(document).on('mouseover','#button2', function() {  // See change in this line.
    $('#button2').css({
        'background-color':'#0FF',
        'width':'400px',
        'height':'200px'
    });
});
Himanshu Upadhyay
  • 6,220
  • 1
  • 13
  • 32
  • You'll need to use commas after each style property, rather than semi-colons. This will give you a syntax error otherwise. – Dan Ellis Jul 27 '17 at 11:03