I have a code where I want a button's text to be stored in a variable and then set a text field's value to the text. Here's my code for the button onclick event:
$('#MyButton').click(function(){
var user1 = $(this).attr("value");
$('#MyTextField').val(user1);
});
And Here is my button and text field:
<input type="text" class="form-control" id="MyTextField" aria-label="..." placeholder="User to send to">
<button id="MyButton" class="btn btn-success" value="Go!">GO</button>
But nothing ever happens. Please help, Thanks in advance
EDIT regarding @Drakes
<script type="text/javascript">
var socket = io.connect('http://192.168.2.18:8080');
$(window).load(function(){
username = "<%= user.local.email %>";
console.log(username);
socket.emit('join', username);
socket.on('userjoino', function(data){
$('#users').empty();
$('#puser').empty();
$.each(data, function(key, value){
$('#users').append('<div class="alert alert-warning">'+key+'</div>');
$('#puser').append('<li class="list-group-item" id="whisper" rec="'+key+'"> <button id="whisper" class="btn btn-success MyButton" value="'+ key + '">'+ key + '</button></li>');
});
var users = data;
console.log(users);
});
$('form').submit(function(e){
e.preventDefault();
var message = $('#msg').val();
if(message.length <= 0){
$('#boxf').append('<div class="alert alert-danger">Message cannot be left empty</div>');
var div = document.getElementById("boxf");
div.scrollTop = div.scrollHeight;
}else{
//var msgob = [nickname + ': ' + message];
//socket.emit('message', msgob);
socket.emit('message', message);
$('#boxf').append('<div class="alert alert-info"> Me: ' + message + '</div>' );
var div = document.getElementById("boxf");
div.scrollTop = div.scrollHeight;
$('#msg').val('');
}
});
socket.on('messages', function(data){
$("#boxf").append('<div class="alert alert-info" >' + data + "</div>");
var div = document.getElementById("boxf");
div.scrollTop = div.scrollHeight;
//document.getElementById('xyz').play();
});
socket.on('disuser', function(data){
$("#boxf").append('<div class="alert alert-info">Server: ' +data+" has left the chat" + "</div>");
var div = document.getElementById("boxf");
div.scrollTop = div.scrollHeight;
});
$('#pm').click(function(e){
var whisp = $('#whisp').val();
var recipent = $('#recipent').val();
console.log(recipent);
socket.emit('whisper', {whisp: whisp, user: recipent});
$('#whisp').val('');
$('#recipent').val('');
return false;
});
$('#whiisper').on('click', '.MyButton', function(){
var user1 = $(this).val();
$('#recipent').val(user1);
});
console.log('hey');
socket.on('whisper', function(data){
console.log(data);
if(data.user === username){
$('#messes').append('<div class="alert alert-info" >' + data.user + ': ' + data.whisp + "</div>");
}else{
console.log('ahaaaaahhh');
}
});
});
</script>
That is my script and here is how I have included JQuery:
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
$('#MyButton').on('click', function(){..}jQueryincluded in your page?Go!gets set to the input!'#whiisper'instead of'#whisper'if that is important