0

I was just wondering how to use the function :not in my situation.

This is jquery. I'm trying to select the elements in the list that are not in the condition mention in the if loop.

Json object received: ["blue", "teleport", "love", "transformers", "blink182", "leap year", "database", "valentines", "bearish", "sting", "a1", "telegraph", "trion", "spa", "property", "jolie", "grace", "santa claus"]

I just want to receive the words without the special characters

function refreshKeywords(e) {
    e.preventDefault();
    var refresh_form = jQuery(e.target);
    alert("HELLO");
    $.ajax({
        url: refresh_form.attr('action'),
        type: refresh_form.attr('method'),
        data: refresh_form.serialize(),
        dataType: 'json',
        success: function(response) {
            var list = response.new_list;
            if (list != "["
            or list != "]"
            or list != "{"
            or list != "}"
            $('#keywords').append("<li>" + list + "</li>");
            },
        });
    }​

Would appreciate if anyone know how to input the :not function into my code. Thank you so much.

3
  • What you have is not valid JavaScript. What are you trying use the :not "function" for? Commented Mar 2, 2012 at 14:13
  • ":not" is a selector filter and has nothing to do with strings. Hard to figure out what you are trying to parse..post a little of the json Commented Mar 2, 2012 at 14:13
  • edited. Sorry for not being clear earlier on. Commented Mar 2, 2012 at 14:19

4 Answers 4

1

The json response is not a human readable string the way you are approaching it. It is an array that you need to loop over to parse vales

success: function(response) {
    var html='';
    $.each(response, function( i, item){
       html+= "<li>" + item + "</li>";
    });
    $('#keywords').append(html)

 },
Sign up to request clarification or add additional context in comments.

Comments

1

There would be no use for the :not selector here. The :not selector is meant for DOM elements.

http://api.jquery.com/not-selector/

Comments

1

:not is a filter, it is used to filter elements out of a selection, e.g:

$("input:not([type='submit'])")

This would select all input elements on the page, except those with a type attribute equal to submit

It is advised to use the not() method for readability instead of the :not filter

What is it you're trying to do and what is not working?

So looking at your updates, you cannot use the word 'or' in the if conditional, use the double-pipe || instead, e.g.

if (list != "[" || list != "]" || list != "{" || list != "}") {
    $('#keywords').append("<li>" + list + "</li>");
}

Except, you probably want to use && instead, because you don't want any of those characters:

if (list != "[" && list != "]" && list != "{" && list != "}") {
        $('#keywords').append("<li>" + list + "</li>");
    }

But...you probably want to do it a different way in any case, instead of working directly with the JSON string, you might want to parse it to turn it into an actual JavaScript object so that you can work with it easier, e.g.

var array = $.parseJSON(response.new_list);

3 Comments

edited my question above. Trying to use a if else loop in jquery itself.
:not is actually a selector, not a filter, for it selects elements rather than filters them.
So it selects things by not selecting them? ;)
0

What you want is a JSON parser. Parse then iterate over the resultant array.

//Cache #keywords outside the loop to make it faster.
var array = JSON.parse(response.new_list), keywords = $("#keywords");
for(var i = 0, len = array.length; i<len; i++) {
    keywords.append("<li>" + array[i] + "</li>");
}

2 Comments

for some reason for loops always make my code not functionable. Any ideas why?
no need to use JSON.parse within a jQuery ajax response, will be handled by core

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.