Skip to content

Instantly share code, notes, and snippets.

@Ahnita Ahnita/posts.js
Last active Aug 1, 2016

Embed
What would you like to do?
To create posts using JS
var main =function() {
$('.btn').click(function() {
var post = $('status-box').val();
$('<li>').text(post).prependTo('.posts');
$('.status-box').val('');
$('.counter').text('140');
});
$('.status-box').keyup(function() {
var postLength = $(this).val().length;
var charactersLeft = 140 - postLength;
$('.counter').text(charactersLeft);
});
}
$(document).ready(main);
var main =function() {
$('.btn').click(function() {
var post = $('status-box').val();
$('<li>').text(post).prependTo('.posts');
$('.status-box').val('');
$('.counter').text('140');
$('.btn').addClass('disabled');
});
$('.status-box').keyup(function() {
var postLength = $(this).val().length;
var charactersLeft = 140 - postLength;
$('.counter').text(charactersLeft);
if(charactersLeft < 0) {
$('.btn').addClass('disabled');
}
else {
$('.btn').removeClass('disabled');
}
});
$('.btn').addClass('disabled');
}
$(document).ready(main);
@Ahnita

This comment has been minimized.

Copy link
Owner Author

commented Aug 1, 2016

This is to create a post with 140 characters and post new succeeding to posts next to each other. Counter resets to 140 after creating a new post.

@Ahnita

This comment has been minimized.

Copy link
Owner Author

commented Aug 1, 2016

In the second version, the button is enabled only if the user types something in the post box. The post button is disabled when the characters is more than 140 characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.