0

I have a jQuery function I am calling with the below code. The function searches for the words shown in the "words:" area in the "QAList" and then makes just those words bold. This is working fine with this code and the words "Demo" and "or" are made bold.

    $("[id$=QAList]").wrapInTag({
      words: ['Demo User', 'or'],
      tag: '<selected>'
    });

I am now trying to pass the "words:" list in from a label or textbox but for some reason I cannot get it to pass the correct information, and I am not 100% sure to see what it is actually passing.

This is how I changed the code to pull from the lblSelectedQA label:

    $("[id$=QAList]").wrapInTag({
      words: [$("[id$=lblSelectedQA]").html()],
      tag: '<selected>'
    });

I even tried it from a textbox in case the label was not working for some reason:

    $("[id$=QAList]").wrapInTag({
      words: [$("[id$=lblSelectedQA]").val()],
      tag: '<selected>'
    });

I am getting what I need from the label and the textbox if I put them in an alert, they return:

'Demo User', 'or'

Do I have incorrect syntax after the "words:" now that I am trying to pass the value from somewhere else?

Thanks!

**Edit in response to iurisilvio's answer below

In response to your answer I changed my code a little bit both in what is populating the 'Demo User', 'or' text to get rid of the single quotes and extra spaces. It now looks like this: Demo User,or and works fine with single results.

I then tried adding the .split(",") which works for one of the values in the array if I add the index [0] or [1], but will not stand alone for all the array items.

Utkanos shows basically the same thing, just adding a way to also get rid of the spaces and single quotes which I no longer need to do.

This is my code now which works for a single result:

    $("[id$=QAList]").wrapInTag({
      words: [$("[id$=lblSelectedQA]").html().split(",")],
      tag: '<selected>'
    });

But when it returns multiple results it won't work unless I add the index, but then will only work for the index that I hard code... not an option since I don't know how many will be in this array.

    $("[id$=QAList]").wrapInTag({
      words: [$("[id$=lblSelectedQA]").html().split(",")[0]],
      tag: '<selected>'
    });

or

    $("[id$=QAList]").wrapInTag({
      words: [$("[id$=lblSelectedQA]").html().split(",")[1]],
      tag: '<selected>'
    });

Is there a way for me to get it to take all the array items without having to loop through them?

Thanks!

**Edit for solution:

I needed to drop the brackets around what I was just adding. I figured that out and came back and Utkanos had just posted the same thing :)

Working code:

    $("[id$=QAList]").wrapInTag({
      words: $("[id$=lblSelectedQA]").html().split(","),
      tag: '<selected>'
    });

Thanks!!

divtag
  • 677
  • 2
  • 6
  • 21
  • Can you provide a link to this `wrapInTag` plugin? Or is this something you made yourself – Ohgodwhy Jul 11 '12 at 15:23
  • [link]http://stackoverflow.com/questions/9794851/find-text-string-in-jquery-and-make-it-bold/9795091#9795091[/link] – divtag Jul 11 '12 at 15:28

2 Answers2

2

This way, your words var is equal ["'Demo User', 'or'"] (one element). If you split your string, you get what you want:

// suposing your_text is right
words: your_text.split(",")

Of course it is a simplified solution. If you want to get something like 'word, 1', 'word 2', you need a better parser to your problem, but it is the way.

iurisilvio
  • 4,868
  • 1
  • 30
  • 36
  • That will leave spaces before words and not remove the single quotes around them. – Mitya Jul 11 '12 at 15:29
  • 1
    Thanks! that is what I was missing. I didn't think about it being one element. I have also found that if I pass it this way I cannot use the single quotes. I have a fiddle that works the way I have it if I drop it to a single element and drop the single quotes. Now I just need to figure out how to get the multiple values which I think the split might do. – divtag Jul 11 '12 at 15:32
  • 1
    I know, but the OP problem is about "passing a string instead an array". I gave him the solution to this problem. Now, he can improve the parser to solve a lot of other problems he will have trying to parse something like that. – iurisilvio Jul 11 '12 at 15:33
  • @divtag so mark it as correct answer. It is good to me and mainly to any other future reference to this post. – iurisilvio Jul 11 '12 at 15:44
  • I am not completely out of the water yet. I have it working better but it still will not see all the array items. I have updated my original question with where I am now. Thanks! – divtag Jul 11 '12 at 15:59
  • 1
    Passing a string rather than an array is non-negotiable if the plugin expects an array. The question is actually: "how do I get some data from a string and turn it into an array such that it is suitable for this plugin?" – Mitya Jul 11 '12 at 16:07
1

The words parameter should be an array. The string 'Demo User', 'or' looks like the print-out from an array that contains those values, but is ultimately a string, not an array.

In other words, you're passing a string that looks a bit like an array, not an actual array.

Assuming the string you're retrieving from the element is, as you say, 'Demo User', 'or', you need to split that into an array by splitting on the delimiter - in your case, the comma and space. You'll also need to remove the ' around the various tokens.

$("[id$=QAList]").wrapInTag({
    words: $("[id$=lblSelectedQA]").val().replace(/'/g, '').split(, ?/),
    tag: '<selected>'
});
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Thanks. I see what you are doing here now that I am a bit further. I have control of how that string is created so I got rid of the single quotes and extra spaces. I am now having trouble with it seeing all the array items as I show in my edits to the question. – divtag Jul 11 '12 at 16:00
  • 1
    Looked at your edit. Using split by itself is not enough as you will need to remove the wrapping single quotes. Another reason the first code snippet in your edit doesn't work is because you're passing an array into an array, which will confuse the plugin. Try simply the code I posted *without* wrapping it in square brackets. (Just realised that my answer also made this mistake; now edited). – Mitya Jul 11 '12 at 16:03
  • Thanks! I just seconds ago tried that and found that as well :) Thanks!! – divtag Jul 11 '12 at 16:05