0

I have spring mvc based java web app. From my server i am sending a array list of strings.

In table i am reading it into data attribute.

something like

   <td id="context"class="hidden" data-contexts='${result.getContext()}'></td>

when i hover on each row i want to show the value of data attribute.

http://jsfiddle.net/3WgTY/ ( expected behaviour)

Now the problem is in debug mode i see the table is like this.

http://jsfiddle.net/4DMqr/ ( current behaviour)

so jquery.data() is not working properly.

I dont know why data-context has got double quotes even though in html i am using single quote. can some one tell how to fix it.

9
  • 1
    .live was removed in jQuery 1.9, you should stop using it. Commented Apr 12, 2013 at 19:10
  • How are those 2 examples generated? That's probably where the issue is. In the 2nd example the double quotes around the attribute is what is causing your problem. Commented Apr 12, 2013 at 19:14
  • 1
    The problem in example 2 is invalid HTML, what is generating the html? This doesn't seem to have anything to do with jQuery or javascript? Commented Apr 12, 2013 at 19:17
  • 2
    @RodrigoAssis Oh but it is json, just not formatted properly to work as an attribute that's using double quotes. Commented Apr 12, 2013 at 19:23
  • 1
    Now i see the first fiddle works... didnt'now that Commented Apr 12, 2013 at 19:24

2 Answers 2

3

The double quotes surrounding your json is breaking the code due to the inner double quotes. You can use single quotes (http://jsfiddle.net/4DMqr/2/), but you'll run into the same problem if your json includes single quotes. to fix it across the board, just replace all " inside the attribute with &quot;

http://jsfiddle.net/4DMqr/4/

<td id="context" data-contexts="[&quot;\&quot;java\&quot; is awesome&quot;,&quot;\&quot;java\&quot; is object oriented&quot;]">java</td>

Also, Id's must be unique, and .live is depreciated/removed in newer versions. please update you code to fix these two (unrelated) issues as well.

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

3 Comments

absolutely. I agree inner double quotes is breaking the outer double quotes. so what is fix for it. as you said single quotes is also not an answer as if single quotes are in string then it will break. how to fix it.
Looks like a fix. thanks. Can you kindly tell me more about what is this $quot or point to some good link on it.
haha, that was a typo. I meant &quot;. And if you don't know what that is, It's an HTML Entity that represents "
2

just curious why you need that looping

  <td id="context" data-contexts='java is awesonme <br> java is oo '>java</td>

whats wrong in this

var context = $(this).find("td#context").data("contexts");   
        $('#contextData').html(context );

may be i dont understand

3 Comments

I agree i can make change on java side to make it string. Still curious how to fix the above issue without changing it to string.
did you see any double quotes if you change the code like i did ?
No i didnt see double quotes.

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.