1

I am trying to make a code that is easy. I am not very good at jQuery, and I'm sorry for this. Need help with this code lol. Ok so what I am trying to do is make a code that has variables in it which in turns are the Usersnames, this is what I have so far

$(document).ready(function() { 
    var $NLG = $('ul li a.mainmenu'); 
 $NLG.filter(':contains("USERNAME")function(){
     .find('.nlgchat');
         //do nothing');
    else
   .remove('.nlgchat');
    });

Yes I know this probably is completely wrong and I am completely lost. But basically what I want done here is if the usernames listed in the :contains variables not to do anything, else if not remove the element completely. Can someone help me to understand this some more, and how to do this code?

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • 2
    Do you see the error? follow the highlight color logic and you'll spot more than one. – Roko C. Buljan Nov 26 '12 at 01:56
  • 1
    syntax errors + logic error -- if `.nlgchat` isn't present, why `remove()` it? – ahren Nov 26 '12 at 01:56
  • .nlgchat does exist. Its a pop up chat I have for a site. But not everyone is allowed to use it. And only way i can try and keep people out is by using this sort of code. – EasyBB Nov 26 '12 at 01:57
  • roXon I believe my function(){ after username is in wrong spot. but like i said im clueless on how to create a function section, then if not that username to .remove the .nlgchat element and its components – EasyBB Nov 26 '12 at 01:58
  • Oh and @ahren you are right I'm sure theres plenty of syntax errors, i'm ashamed. I know jQuery but I am not advanced. I've been self teaching myself this for only maybe 4months I am sorry for the bad scripting. I'm ashamed – EasyBB Nov 26 '12 at 02:00
  • 1
    All good, everyone has to start somewhere! That's what makes SO such a good resource and community. There are a few answers below for you to review. – ahren Nov 26 '12 at 02:01
  • Thank you @ahren I appreciate that. Some of the other SO users I've ran into are not so understanding. I could go to some of these other hack backwards forum sites. But rather be here to be honest. Again thank you – EasyBB Nov 26 '12 at 02:05

2 Answers2

2

I believe this is what you're looking for:

$(document).ready(function() { 
    if ( ! $('ul li a.mainmenu:contains("USERNAME")').length ) {
        $('.nlgchat').remove();
    }
});

though I'm sure you could optimize that selector.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • excuse me @joseph silber can I ask what a selector is? Also this should work even if the username is wrapped in say LOG OUT [ USERNAME ] ? – EasyBB Nov 26 '12 at 02:04
  • 1
    @user1760670 - Sure. The selector is the string that you pass to the `$` function. – Joseph Silber Nov 26 '12 at 02:05
  • $('ul li a.mainmenu:contains("USERNAME")') thats a selector and $('.nlgchat') are selectors. So meaning in the sense whatever you are trying to select LOL? I'm trying to get a better understanding thats all – EasyBB Nov 26 '12 at 02:06
2

If I understood correctly:

  • hide chat by default
  • if match found = show chat

jsBin demo

$(document).ready(function() {

  $('.nlgchat').hide(); // or do it rather with CSS...

  var usernames = ['roko','roky','roxon']; // remove 'roko' and no match will be found
  var nameslist = []; // to match against server usernames

  $('ul li a.mainmenu').each(function(){ 
    var name = $(this).text().toLowerCase();
    nameslist.push(name); // dana,roko,john
  });

  $.each(usernames, function(i,v){
     if( nameslist.indexOf( v ) > -1 ){
        $('.nlgchat').show(); 
        return false;
     }else{
        $('.nlgchat').hide();
     }
  });

 });
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • This actually just may work.I'll check. If so you understood 100% bout the easy var usernames :) – EasyBB Nov 26 '12 at 02:13
  • :) hehehe you can use more values to be checked for, example if you have many boring users, you just fill in their names and it will hide the chat room and also always checking for `lowerCase` – Roko C. Buljan Nov 26 '12 at 02:15
  • For some reason .nlgchat is still visible? – EasyBB Nov 26 '12 at 02:15
  • Oh wait. This code is making it for the users i pick to remove it from. I would only want to show for certain users. Any way to manipulate this code so it automatically .remove for all users except the ones in the var usernames? – EasyBB Nov 26 '12 at 02:17
  • wee flaw in your logic roXon - all usernames can't match. Also had the same flaw in my logic, actually. – ahren Nov 26 '12 at 02:19
  • Yes sir that worked right away :) Also this is not jQuery is it? this would be the basic javascript... see i skipped javascript and went to jquery. probably a bad idea right – EasyBB Nov 26 '12 at 02:20
  • @user1760670 than hide by default your chat and do `$('.nlgchat').show();` like: http://jsbin.com/egusun/2/edit – Roko C. Buljan Nov 26 '12 at 02:21
  • ahren what happened to your post? lol. I was giving you ups on it. I believe in passing the love to whom ever helps me. Just have to wait for the acceptance and i'll give the check mark. – EasyBB Nov 26 '12 at 02:22
  • ok roXon I see what you did there. What happened? Was your code defective?? It worked for the most part. – EasyBB Nov 26 '12 at 02:23
  • the new code you created roXon, I'm sorry for being a pest. But it will not show the element that is needed to be seen...? – EasyBB Nov 26 '12 at 02:29
  • 1
    @user1760670 - it's okay, you can give it to roXon or Joseph. My code was flawed so I removed my post. – ahren Nov 26 '12 at 02:33
  • ooo ok. well thanks for being understanding ahren! appreciate that! – EasyBB Nov 26 '12 at 02:33
  • would I add this to the part //do something $('.nlgchat').add(); or .show();? – EasyBB Nov 26 '12 at 02:38
  • its not a problem I'm patient – EasyBB Nov 26 '12 at 02:53
  • ok so it changed again lol. I know I'm a pain in the butt. I liked the way it would remove. Because then there is no way someone can use inspect element and change it to show. .remove is great. Any way we can go back to that way? – EasyBB Nov 26 '12 at 02:55
  • No. It alerts. No alerts. Just instead of .hide originally. I want if the users in the var do not exist to then in turn .remove the .nlgchat see what I mean? – EasyBB Nov 26 '12 at 03:08
  • C'mooon the alerts are just for example! :) just remove the alerts and do your stuff there show / hide or whatever you need – Roko C. Buljan Nov 26 '12 at 03:09
  • lol I know the alerts were example. I am sorry. I'll stick with what you have now. Just wanted it removed instead thats all. – EasyBB Nov 26 '12 at 03:11
  • ok so what I did was .show for if matched else .remove if no match :) – EasyBB Nov 26 '12 at 03:13
  • code is not working for me on my site because the names are wrapped in Log Out [ USERNAME ] I tried manipulating your code to work for me seems like nothing is working... – EasyBB Nov 26 '12 at 03:31
  • What means `are wrapped in Log Out [ USERNAME ]` can you be please more specific? – Roko C. Buljan Nov 26 '12 at 03:34
  • The text for the a href is Log out [ USERNAME ] so when I change the text in the jsbin to that it does not work – EasyBB Nov 26 '12 at 03:35
  • can you modify the output? Just add a `data-name="someUserName"` to the `a` elements ? – Roko C. Buljan Nov 26 '12 at 03:38
  • If no, than do : http://jsbin.com/egusun/20/edit to retrieve the names by splitting the text string by spaces `' '` and retrueving the `3rd` array key (the name) This approach could be a failure if "space" is allowed for usernames – Roko C. Buljan Nov 26 '12 at 03:40
  • like your second one. No spaces are allowed in the user name. Can't modify the data in the href. the usernames and links are generated by php of the hosting site. I'll use your second one sense it makes sense. Though you are saying if the user name is USER NAME then it won't work? – EasyBB Nov 26 '12 at 03:45
  • Hey this code does not seem to accept more than one username? Why so? I have 10 members that need to be listed in the var namelist="" – EasyBB Nov 30 '12 at 00:18