0

I'm trying to use this to clean up my select options as they come in from AJAX. Many of them have an "_" in them and I want to remove them:

$.each(data, function(i, data) {
    $('#first_select2').append("<option>" + data.COLUMN_NAME + "</option>");               
    $('#first_select2').replace(/_/g, " ");
});

The replace works fine if I try to use it outside of an each. If I use it in the .each, I get this error:

Uncaught TypeError: undefined is not a function

Is there a way to do what I need here?

2
  • 1
    replace _ in data.COLUMN_NAME? if so, do it before you append. Commented Mar 27, 2015 at 15:13
  • 1
    $('#first_select2') <-- This is a jQuery object but .replace(/_/g, " ") <-- "replace" works on strings. Commented Mar 27, 2015 at 15:17

2 Answers 2

1

If you wanted to replace _ in data.COLUMN_NAME, do it before you append, like below.

$.each(data, function(i, data) {
    $('#first_select2').append("<option>" + (data.COLUMN_NAME.replace(/_/g, " ")) + "</option>");
});

As later, you were trying to replace _ in a select box instead of options's text.

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

Comments

0

Should you using spesific datatype ajax, like as datatype:'json', so you will get return clean result..

If you had using json, you can replace in your php (str.replace) code and then pass to echo json_encode(your result), and than place that on your select option.

Comments

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.