Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:13:28+00:00 2026-06-07T05:13:28+00:00

I’m trying to dynamically set a div id through jQuery. Yesterday, it seemed like

  • 0

I’m trying to dynamically set a div id through jQuery. Yesterday, it seemed like the code was working, but today, it’s complaining.

The loop in question is:

    socket.on('nicknames', function (nicknames) {
    $('#nicknames').empty().append($('<span>Online: </span>'));
      for (var i in nicknames) {
    $('#nicknames').append($('<div class="chatNickname" id="' + nicknames[i] + '">').text(nicknames[i]));
      }
    });

The problem is, when I set the ID dynamically, the list of users is only one – the local user. If I set the ID to something static, it works, and the entire list of users is visible.

I think it’s a syntax issue, but from this page (http://www.dynamicdrive.com/forums/showthread.php?t=36730), I think I’m doing it right.

Thanks for your help!

The whole code is:

<script src="http://<?php print $_SERVER['SERVER_ADDR'] ?>:8080/socket.io/socket.io.js"></script>
<script>
  (function($){
    var myNick = 'me';
    var newlyJoined = true;
    var socket = io.connect('http://<?php print $_SERVER['SERVER_ADDR'] ?>:8080');

    socket.on('connect', function () {
      $('#chat').addClass('connected');
    });

    socket.on('announcement', function (msg) {
      $('#lines').append($('<p>').append($('<em>').text(msg)));
    });

        socket.on('nicknames', function (nicknames) {
        $('#nicknames').empty().append($('<span>Online: </span>'));
          for (var i in nicknames) {
        $('#nicknames').append($('<div class="chatNickname" id="' + nicknames[i] + '">').text(nicknames[i]));
          }
        });

    socket.on('user message', message);
    socket.on('reconnect', function () {
      $('#lines').remove();
      message('System', 'Reconnected to the server');
    });

    socket.on('reconnecting', function () {
      message('System', 'Attempting to re-connect to the server');
    });

    socket.on('error', function (e) {
      message('System', e ? e : 'A unknown error occurred');
    });

    socket.on('chat log', function(chatlog) {
      if (newlyJoined) {
    var i = 0;
    for (stamp in chatlog) {
      if (chatlog[stamp].type == 'user message') {
    var ts   = tstamp(stamp);
    var nick = chatlog[stamp].nick
    var msg  = chatlog[stamp].msg
    message(ts, nick, msg);
    i++;
      }
    }
    if (i > 0) {
      $('#lines').append($('<hr>')).append($('<small style="text-align:center; display:block; color: #888;">').text('Chat messages posted within the past half hour appear above this line.')).append($('<hr>'));
      $('#lines').get(0).scrollTop = 10000000;       
    }
    newlyJoined = false;
      }
    });

    function message (msg_time, from, msg) {
      $('#lines').append($('<p>').append($('<small>').text(msg_time)).append($('<b>').text(from), linkify(msg)));
    }

    function linkify(inputText) {
      //URLs starting with http://, https://, or ftp://
      var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
      var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

      //URLs starting with www. (without // before it, or it'd re-link the ones done above)
      var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
      var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

      //Change email addresses to mailto:: links
      var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
      var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

      return replacedText
    }

    function tstamp(stamp) {
      var currentTime = new Date(); 
      if (typeof stamp != 'undefined') {
    currentTime.setTime(stamp);
      }
      var days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat');
      var day = currentTime.getDay();
      var hours = currentTime.getHours();
      var minutes = currentTime.getMinutes();
      if (minutes < 10) {
    minutes = "0" + minutes;
      }
      if (hours > 11) {
    var ap = 'p';
      }
      else {
    var ap = 'a';
      }
      if (hours > 12) {
    hours = hours - 12;
      }
      return "["+ days[day] + " " + hours + ":" + minutes + ap + "m] ";
    }

    $(document).ready(function() {

      $('#nicknames').on('click', '.chatNickname', function(){ 
   alert("hello");
});

      $('input#message').focus(function() {
    if ($(this).val() == 'Type your chat messages here...') {
      $(this).val('');
    }
      });

      $('input#show-timestamps').click(function() {
    if ($(this).is(':checked')) {
      $('#messages p small').show();
    }
    else {
      $('#messages p small').hide();
    }
      })

      socket.emit('nickname', '<?php print $username ?>', function (nick) {
    if (nick != 'me') {
      myNick = nick;
      socket.emit('get log');          
      return $('#chat').addClass('nickname-set');
    }
      });

      $('#send-message').submit(function () {
    message(tstamp(), myNick, $('#message').val());
    socket.emit('user message', $('#message').val());
    clear();
    $('#lines').get(0).scrollTop = 10000000;
    return false;
      });

      function clear () {
    $('#message').val('').focus();
      };

    });
  })(jQuery);
</script>
<div id="chat">
  <div id="messages">
    <div id="nicknames">
    </div>
    <div id="lines">
    </div>
  </div>
  <form id="send-message" autocomplete="off">
    <input id="message" type="text" value="Type your chat messages here..." autocomplete="off" />
    <button>Send</button>
  </form>
</div>
<small><input id="show-timestamps" type="checkbox" checked="checked" /> Show timestamps</small>a
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:13:29+00:00Added an answer on June 7, 2026 at 5:13 am

    Try the following:

    socket.on('nicknames', function (nicknames) {
        $('#nicknames').empty().append($('<span>Online:</span>'));
        for (var i in nicknames) {
            var attrs = {
                'class': 'chatNickname',
                'id': nicknames[i],
                'text': nicknames[i]
            };
            $('#nicknames').append($('<div>',attrs));
        }
    });
    

    demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.