1

I was creating a html that would create a div and copy the things in input to that div with a button click.

Like this:

HTML

<input type="text">
<button>Copy</button>
<p></p>

Script

$('button').click(function() {
   var div = document.createElement('div');
   $('p').append(div);
   $('div').html($('input').val());
});

At it worked perfectly

And then i added class to it.

It looks like this:

$('button').click(function() {
   var div = document.createElement('div');
   div.className = "Someclass";
   $('p').append(div);
   $('div').html($('input').val());
});

It worked fine as well. But when i click the button it copies the text to all the div's Instead of a single one

How can i create a button that creates different div with different class and which copies text from <input> to the newly created div

I found on net that i can use the i++ thing in javascript for different class.
But that code is not working properly. Please tell me what is the mistake here.

My code looks like this:

$('button').click(function() {
   var i = 0;
   var div = document.createElement('div');
   div.className = "Someclass" + i++;
   $('p').append(div);
   $('div').html($('input').val());
});
2
  • Actually you define i on every click, so the className will always be Someclass0. Furthermore, the $('div') selector will catch all the div's in your document and will overwrite all of their content Commented Jun 5, 2016 at 10:51
  • How can i solve it?? Commented Jun 5, 2016 at 10:53

6 Answers 6

2

You can do it like this:

var clickCount = 0;

$('button').click(function() {
  var html = $('input').val();
  var classname = 'someclass' + clickCount++;
  $('<div>').addClass(classname).html(html).appendTo('p');
});
Sign up to request clarification or add additional context in comments.

Comments

1
  1. take out i from click event
  2. select new div with its corresponding class name .Someclass[i]

change your code like this:

var i = 0;
$('button').click(function() {

    var div = document.createElement('div');
    div.className = "Someclass" + i;
    $('p').append(div);
    $('.Someclass' + i).html($('input').val());
    i++;
});

Comments

1

Rather use .addClass jquery method:

$( "div name" ).addClass( "yourClass" );

Comments

1

Its doing that because you have this line

 $('div').html($('input').val());

You can change it to

 $('p div').html($+'input').val());

Comments

1

You should give the precise class name like 'Someclass'+i with " i " the last index of the class you added.

$('button').click(function() {
   var i = 0;
   var div = document.createElement('div');
   div.className = "Someclass" + i++;
   $('p').append(div);
   $('Someclass'+i).html($('input').val());
});

Your code looks like it will add only one div by click and that div will be given the value of input.

Comments

1

You are appending html to all div present.Do this add content to it before appending

var  i=0;
$('button').click(function() {
   var div = document.createElement('div');
   div.className = "Someclass" + i++;
   $(div).html($('input').val());
  $('p').append($(div));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<button>Copy</button>
<p></p>

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.