harikrishnan.n0077
harikrishnan.n0077

Reputation: 2057

Event does not work using jquery

<div id="form">
<form action="">
<input type="button" id="sub"/>
</form>
</div>

When clicking the submit button content will be changed using jquery.

Jquery code:

$(document).ready(function() {
    $('#sub').live('click', function() {
        var reg = "<form action=''><input type='text' id='text'/><input type='button' is='sub2'/></form>";
        $('#form').replaceWith(reg);
    });
});
$(document).ready(function() {
    $('#sub2').live('click', function() {
        $('#text').text("Some content");
    });
});

But my problem is the content does not get written into input box. How can i do that plz help? Also how to change the background color of input box with id text?

Upvotes: 2

Views: 61

Answers (2)

ThiefMaster
ThiefMaster

Reputation: 318498

Your code contains syntax errors. You use double quotes inside the var reg = "..."; string (action="") which need to be escaped or written as '

You also need to use .val() to get/set the value of an input element instead of .text()

There is also no reason to use two document ready functions - just put everything in the same one.

Last but not least, live() is deprecated; use on() instead (see the jQuery docs on how to use it properly)

Upvotes: 2

Adil
Adil

Reputation: 148110

But my problem is the content does not get written into input box.

You need to call val() instead of text() as text() is not defined for input type of text.

$(document).ready(function(){
   $('#sub2').live('click', function () {
      $('#text').val("Some content");
   });
});

Upvotes: 2

Related Questions