Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:28:07+00:00 2026-06-14T03:28:07+00:00

I have a jQuery library that wants you to pass it a JavaScript object

  • 0

I have a jQuery library that wants you to pass it a JavaScript object as an option. For example, this works:

//Hardcoded JavaScript object
var myData = { "A" : "1", "B" : "2", "C" : "3" };

//Call to jQuery with object as an option
$("#mySelectorId").myJqueryFunction({values: myData});

I need the myData object to not be hardwired like that.

I can easy get the “A to 1, B to 2, C to 3” structure in my Java code, put it in a Map (or some other object), and add it to the request via request.setAttribute("myData", myMap);. I am just not sure how to convert the Map (etc) into the needed JavaScript object. Here is what I have tried.

//Java
Map<String, String> myMap = new TreeMap<String, String>();
map.put("A", "1");
map.put("B", "2");
map.put("C", "3");
request.setAttribute("myMap", myMap);

//JSP
<c:set var="myData" value="{" />
<c:forEach var="mapEntry" items="${myMap}" varStatus="counter">
    <c:set var="myData" value="${myData}'${mapEntry.key}' : '${mapEntry.value}'" />
    <c:if test="${counter.count < fn:length(myMap)}">
        <c:set var="myData" value="${myData}, " />
    </c:if>
</c:forEach>
<c:set var="myData" value="${myData}}" />

<script>
    var myData = '${myData}';
    $("#mySelectorId").myJqueryFunction({values: myData});
</script>

This works until the myJqueryFunction reaches a point where it tries to do something like this:

var i = values[code];

Essentially, it is trying to reference a particular variable on the object (e.g. trying to get the value of “B”, which is 2). Note that code is a function parameter that holds the A/B/C value.

I have tried messing around with including or not including the quotes around ${mapEntry.key} and things like that, but nothing seems to work.

Does anyone know how to get this working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:28:08+00:00Added an answer on June 14, 2026 at 3:28 am

    One solution is to use AJAX/JSON.

    In a NEW controller (lets have it map to /MyControllerUrl), use a List and fill it with objects that have both the letter and the number of our objects (lets call this new object MyClass).

    List<MyClass> myList = new ArrayList<MyClass>();
    myList.add(new MyClass("A", "1"));
    myList.add(new MyClass("B", "2"));
    myList.add(new MyClass("C", "3"));
    request.setAttribute("myList", myList);
    

    The above is all that is in the new controller. And the returned JSP has only the following.

    <c:out value="[" />
    
    <c:forEach var="listItem" items="${myList}" varStatus="counter">
        {"letter": "${listItem.letter}", "number": "${listItem.number}"}<c:if test="${counter.count < fn:length(myList)}">,</c:if>
    </c:forEach>
    
    <c:out value="]" />
    

    Finally, in the original JSP, have the following.

    <script>
        var myJsList= { };
        $.ajax({
            url: '${pageContext.request.contextPath}/MyControllerUrl',
            dataType: 'json',
            async: false,
            success: function(objectInfo) {
                $.each(objectInfo, function(key, object) {
                    myJsList[object.code] = object.label;
                });
            }
        });
    </script>
    

    Note that I used .ajax instead of .getJSON so that I could set async: false so that the variable would be immediately ready for reference.

    I don’t love this option, as it requires 2 calls instead of 1. So I’ll wait and see if there are any other ideas suggested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a third party JQuery library that does this: var $this = $(this);
I have a master page that adds the jquery library via a registerclientscriptinclude: Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
I'm looking for a (ideally jQuery plugin) Javascript library that will let me provide
The JQuery PHP Library creates an object $.php which works similarly to $.ajax .
can somebody tell me what is jQuery plugin? I have used the jQuery library
I have built a an app in PhoneGap/Cordova and used the JQuery library for
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have jQuery to select all font elements that are children of the element
I have a jquery based slider on the page. This slider calls a function
I have used jQuery library to find out height of a div . Below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.