-2

I want to add a transparent input text field in the centre of DIV #mainC each time the button is clicked.

JsFiddle

I tried doing this with jquery but did not succeed.

JonaNathali
  • 177
  • 3
  • 12

4 Answers4

0

use append():

$("#abc").click(function() {
    $("#main").append('<input type="text" />');
});

demo: http://jsfiddle.net/6nkd5/5/

Paradoxis
  • 4,471
  • 7
  • 32
  • 66
meni181818
  • 1,103
  • 1
  • 7
  • 11
0

Well there's multiple things wrong with your code:

  • You use the same ID multiple times (ID's can only be used once, classes can be used multiple times).
  • Calling a function from a external JavaScript source will result in the function not running.
  • You're trying to use jQuery when jQuery is not loaded
  • $('<input type="aText" id="aText">').appendTo('#main') is not a valid jQuery, instead try to use: $('#main').append('<input type="text" id="aText">');
  • aText is not a valid input type attribute value
Paradoxis
  • 4,471
  • 7
  • 32
  • 66
  • Why is `$('').appendTo('#main')` invalid? When `$()` takes HTML as an argument, it creates a DOM element and returns a jQuery wrapper for that element, which has the method `appendTo`. So what's wrong with this code? – rhino Jul 31 '14 at 19:47
0

There is no library selected at the upper left corner in jsfiddle. Try to select jquery there.

Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36
0

Use append() and try not to specify onclick inline. You can register the event with jQuery when document is ready. Registering methods this way is preferred because you separate logic from your HTML (Check unobtrusive javascript).

This is how you would do it preferably:

$('#abc').on('click', function(){
    $('#mainC').append('<input type="aText" id="aText">')
})

Check also jsfiddle

The problem you had with your jsFiddle was also that no jQuery script was selected.

Community
  • 1
  • 1
the berserker
  • 1,553
  • 3
  • 22
  • 39