Repopulate data with jQuery and JavaScript

I have a div element as shown below in an HTML page:

Whenever I click on any of the div elements, I save the text within the span tag to a database. Upon returning to the same page, I retrieve the saved text and try to repopulate the clicked view based on that text.

I have included all the code I used, but I am having trouble populating the data.

<div class="panel-body">
    <div class='score score-exel'>
        <div class='arrow'></div><span class='scoreText'> 750-850 Excellent</span>
    </div>
    <div class='score score-good'>
        <div class='arrow'></div><span class='scoreText'> 700-749 Good</span>
    </div>
    <div class='score score-ok'>
        <div class='arrow'></div><span class='scoreText'> 650-699 OK</span>
    </div>
    <div class='score score-fair'>
        <div class='arrow'></div><span class='scoreText'> 600-649 Fair</span>
    </div>
    <div class='score score-poor'>
        <div class='arrow'></div><span class='scoreText'> 300-599 Poor</span>
    </div>
    <div class='btnHold'>
        <button class='btn btn-success idkBtn'>I Don't Know</button>
        <button class='btn btn-success submitBtn'>Submit</button>
    </div>
</div>

When a user clicks on a div element, the selected value is stored in a variable called 'userCreditScore' in the following piece of code:

$('.score').bind('click', function(){
    $('.arrow').hide();
    $(this).find('.arrow').fadeIn();
    userCreditScore = $(this).find('span').text().toString();
});

In this section of code, I attempt to populate the data based on the retrieved response, but I am facing challenges in achieving this:

function fillData(){
    var response = nav.scorm.getResponse('CREDIT_SCORE');
    if(response != ''){
        var jsonData = jQuery.parseJSON(response);

          *** tried various approaches like the one below but did not yield desired results
//$('.score').find('span.scoreText:contains('+jsonData.CREDIT_SCORE+')').parent().css('display', 'block');
        //$('.score').find('span.scoreText:contains('+jsonData.CREDIT_SCORE+')').parent().click();
    }

Answer №1

It seems like the issue lies in the callback missing from your nav.scorm.getResponse method, especially if it involves an AJAX call. Make sure to review the code that you have commented:

//here we do the request
var response = nav.scorm.getResponse('CREDIT_SCORE');

//this line and is executed immediately, while 'response' var is still undefined
    if(response != ''){

Depending on how the code in nav.scorm.getResponse is structured, you may need something like this:

nav.scorm.getResponse('CREDIT_SCORE', function(response){
  if(response != ''){
    //insert into DOM
    console.log(response);
  }
});

UPDATE

The issue was related to the spaces in the score texts, ' 600-649 Fair' will not work, but '600-649 Fair' should.

You can refer to this fiddle for more clarity - https://jsfiddle.net/shershen08/v3gxy8rL/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with applying various textures to a Three.js cube

Hey everyone, I'm trying to add 6 textures on each face of my three.js cube following this thread: Three.js - Using CubeTextureLoader to create a different image on each face of a cube Below is the code I am using: scene = new THREE.Scene(); var ...

Tips for securing a navbar in material ui

https://i.stack.imgur.com/CYfmQ.png In my current React project, I am facing an issue with aligning components within the Material-UI AppBar Component. My aim is to achieve a seamless and perfectly straight alignment for three key elements: a logo image, ...

Warning: The core schema has detected an unknown property `color` for the component or system `undefined` in Aframe + Vuejs. This issue was flagged within 10 milliseconds in

I am facing some challenges trying to integrate Aframe and vuejs seamlessly, as the console is displaying warning messages. It seems like Aframe is validating the attribute values before vue has a chance to modify them. Warning messages core:schema:warn ...

Updates to object properties are not appearing in Vue component

I am facing an issue with a Vue component that has a single prop, which is an object. Despite changing a property in this object, it does not reflect the update in the Vue template. Below is a simplified version of the component: <template> <p ...

jQuery.ajax Failure Response

When using MVC on the server side and calling a function with jQuery.Ajax that returns JSON, an exception is being thrown. How can I invoke the error handling function of Ajax by sending something back in the return JSON function? MVC Function public Jso ...

Acquire dynamically loaded HTML content using Ajax within a WebView on an Android

I have been attempting to extract the content of a web page on an Android platform. Despite trying JSoup, I faced a limitation with ajax support. As an alternative, I am endeavoring to embed the URL within a hidden web view and retrieve the HTML in the on ...

Discover the process of retrieving an element by its ID when dealing with dynamically generated IDs in AngularJS

I am creating a list using the following code snippet: <div ng-repeat="list in fileUploadList"> <div id="{{list}}Upload"></div> </div> Within the controller, I need to retrieve the element by ID, so I am utilizing this line of ...

What is the process for sending text messages in a local dialect using node.js?

Having an issue with sending bulk SMS using the textlocal.in API. Whenever I try to type a message in a regional language, it displays an error: {"errors":[{"code":204,"message":"Invalid message content"}],"status":"failure"} Is there a workaround for se ...

Problem with select field for custom shipping method at WooCommerce checkout section

A custom WooCommerce plugin was created to integrate SF Express service points into the Checkout Block page. While the dropdown appears correctly, the selected option is not being passed to PHP, resulting in the order notes not reflecting the chosen servic ...

Determine the frequency of duplicate elements by analyzing an array of both numbers and strings

I need to write a function that takes an array element and a different array string as parameters, converts them into strings, and then counts the number of duplicate elements. function count5numbers1(arr, arr1) { let m1 = arr.toString().match(/[5]/gi ...

Tips for choosing an ID in Jquery to use in an if statement

I'm trying to create a form selector that will display different outputs depending on the selected option. However, I'm having some trouble getting it to work. Below is my current code: <!doctype html> <html> <head> <meta ch ...

Switching script code to composition API(setup) in Vue3

I have implemented the code below in a page. <script> export default { name: 'FaqSection', props: { content: { type: Object, required: true, }, }, data() { return { scrollArgs: { behavior: &apos ...

Upgrade the WordPress light editor to the advanced version

After developing a script to upgrade the WordPress editor on a specific page from light mode to Advanced once a user clicks the Unlock button and confirms their desire to make the switch, an issue arose. Despite deducting 5 coins from the user's balan ...

Unforeseen behavior in the definition of requirejs

This first snippet of code is functional: define([ 'jquery', 'underscore', 'backbone' ], function($, _, Backbone,home_template) { var HomeView = Backbone.View.extend({ render: function() { ...

JavaScript throws an error because document is not defined

view image description hereWhy am I encountering an error after installing node.js and running various JavaScript commands? It seems like the issue is not with my code, but rather that node.js is failing to establish a connection with the server. view ima ...

Activate the radio button with jQuery when the event occurs

I have been experimenting with using HTML and JS along with the functionality provided by bootstrap-switch.org to toggle between two groups of radio buttons within a form. However, I am encountering difficulty in setting the first radio button in the secon ...

Issue with Bootstrap DIV not extending to full height when applying background-color

I have encountered numerous posts on this topic, but none of the solutions provided have worked for me. I must admit that I struggle to grasp CSS concepts, although I am making an effort to learn. Here is the code I am currently using: <!DOCTYPE html& ...

Can anyone provide instructions on how to show or hide alerts in Bootstrap 5?

I have a snippet in my html file that looks like this: <div class="alert alert-success alert-dismissible"> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> <strong> ...

Obtain access to a Java list of objects using JavaScript or ExtJS

Interested in working on a list of objects with JavaScript, potentially using ExtJS as well in the project. There are two lists in the JSP file (modelList and categoryList) forwarded by the controller, and the goal is to access data in both lists. Is this ...

Can the top header stay fixed to the top of the screen even when scrolling down?

Code snippet: http://jsfiddle.net/R3G2K/1/ In my project, there are multiple divs with content and each div has a header. My goal is to make the last header that goes out of viewport "sticky" or fixed at the top. I have explored various solutions for thi ...