Changing the .load function based on user input

Can I replace a .load text with one that can be updated by a user using form input or similar method? My goal is to create a code that retrieves data using unique div IDs (specific to each employee) containing information within tables across various HTML documents over the years.

For instance:

.load('day.html #empId')

The "day" and "empid" sections of .load should be editable by the user.

[Link] [ID] Submit

Upon submission, the script will execute.

This is the adjustable part of the script:

            $('a').click(function() {
                $('#metrics').load('day.html #empId', function() {
                    $(this).hide()
                            .appendTo('#main')
                            .slideDown(500);
                    });

                    return false;
            })
        });

I hope that explanation was clear enough (I'm new to jQuery).

Answer №1

Retrieve the selected options from two form select elements, combine them into a string, and use them as arguments in the .load function in jQuery.

$('a').click(function() {
    var ds = $('#datasources option:selected').text();
    var empId = $('#empIds option:selected').text();
    $('#metrics').load(ds + '.html #' + empId, function() {
        $(this).hide()
        .appendTo('#main')
        .slideDown(500);
    });

    return false;
});

This is the HTML that will be affected:

<div id="main">
        <h1 id="metrics">Metrics</h1>
</div>

JSFiddle: http://jsfiddle.net/a8dTR/ (Open the console to observe the process. Note that it may throw an error on JSFiddle due to loading restrictions, but you can still verify the correct arguments being passed.)

Answer №2

Success! Despite possibly being frowned upon as bad practice, I managed to achieve the desired result by trying it with multiple documents and IDs. If anyone has a better suggestion, I'm open to hearing it!

While my implementation may be simple, here is the code in case others are interested in doing something similar:

I reintroduced $<'div id= metrics'/> to create a new div tag appended to #main.

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function () {

$('a').click(function() {
var pD = $('#period').val();
var wD = $('#week').val();
var dD = $('#day').val();
var eD = $('#empId').val();
    $('<div id"metrics" />').load(
              pD
            + wD
            + dD    
            + eD, function(){
        $(this).hide()
                .appendTo('#main')
                .slideDown(1000);
    });

});

});
</script>

I then removed the .html # from the function and simply added it as a value to the #empId options.

<form>
<select class="dates" id="period">
    <option value="p00"class="emp">Period</option>
    <option value="p01">1</option>
    <option value="p02">2</option>
    <option value="p03">3</option>
    <option value="p04">4</option>
    <option value="p05">5</option>
    <option value="p06">6</option>
    <option value="p07">7</option>
    <option value="p08">8</option>
    <option value="p09">9</option>
    <option value="p10">10</option>
    <option value="p11">11</option>
    <option value="p12">12</option>
    <option value="p13">13</option>
    <option value="p14">14</option>
</select>
 <select class="dates" id="week">
    <option Value="w00"class="emp">Week</option>
    <option value="w01">1</option>
    <option value="w02">2</option>
    <option value="w03">3</option>
    <option value="w04">4</option>
    <option value="w05">5</option>
    <option value="w06">6</option>
</select>
 <select class="dates" id="day">
    <option value="d00"class="emp">Select Day or Leave Blank for Week</option>
    <option value="d01">1</option>
    <option value="d02">2</option>
    <option value="d03">3</option>
    <option value="d04">4</option>
    <option value="d05">5</option>
    <option value="d06">6</option>
    <option value="d07">7</option>
</select>
<select id="empId">
 <option class="emp">Employee ID</option>
 <option value=".html #JXS0001">John Smith</option>
</select>

<a href="#">Load Metrics</a>
</form>

</div>

<div id="main">
<h1>Metrics</h1>

</div>

There's still work to be done (such as making the employee ID dynamic and editable from a separate HTML file), but that's the exciting part (and I know how to do it now!). Many thanks to @bloodyKnuckles for assisting this newbie.

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

Error: An unexpected 'if' token was not caught

Encountering an error related to the question title while working with this code snippet: $LAB.queue(function setup() { FB.init({ appId: '00000000', status: true, cookie: true, xfbml: true, logging: &ap ...

The dynamic design of the webpage allows for a fluid layout, where the navigation bar smoothly adjusts its position when

I anticipate receiving criticism from certain users for not conducting enough research. However, I have dedicated two days to this issue and the lack of guidance is starting to frustrate me. Therefore, I offer my apologies if this question has already been ...

The debounced function in a React component not triggering as expected

I am facing an issue with the following React component. Even though the raiseCriteriaChange method is being called, it seems that the line this.props.onCriteriaChange(this.state.criteria) is never reached. Do you have any insights into why this.props.onC ...

How to utilize a PHP array within a Vue.js template

I have been exploring the realms of Laravel and vue.js recently and have encountered a challenge. Within my Laravel model, I have a PHP method that retrieves data from a database and organizes it into objects stored in an array. Now, my goal is to access t ...

The Safari browser restricts interaction with password inputs but allows interaction with other types of input fields

My password input field is styled like this: <input class="genericButton" id="login-password" type="password" name ="password" placeholder="Password"> While everything functions correctly in Chrome, I encounter an issue with Safari. When I try to i ...

Having trouble obtaining outcomes from Cloud Code in Parse

After struggling with this issue for quite some time, I have reached a point where I feel the need to seek help. I am working on a cloud code function that is supposed to retrieve a list of categories along with their respective products. Each category con ...

The use of Ajax in jQuery can sometimes result in the PHP isset function being unable to detect

I'm seeking assistance with my code. I am attempting to retrieve values and then send them to a PHP script via AJAX using jQuery. While I can extract the values, there appears to be an issue that I can't identify. Any help you can offer would be ...

Transform a group of objects in Typescript into a new object with a modified structure

Struggling to figure out how to modify the return value of reduce without resorting to clunky type assertions. Take this snippet for example: const list: Array<Record<string, string | number>> = [ { resourceName: "a", usage: ...

When implementing datatables in Rails, the Id field integrated with "accepts_nested_attributes_for" functionality suddenly vanishes

Situation: Currently, I am utilizing simple_form to exhibit nested attributes of an association in rails. In order to enhance the edit form for the nested association, I have integrated the jquery-datatables gem using simple_fields_for. Here is a glimpse ...

creating an interactive element that seamlessly integrates with a dynamic background image slideshow

Struggling to make this work correctly as a newbie in javascript. I need the background image to slide upon hover and stay active on its selected div when clicked. The html, css, and javascript I have currently work fine - when the user clicks on a div, a ...

Is there a way to bypass the initial result when using document.querySelectorAll?

this is my own unique html content <div class="content-body"> <table style="text-align:center;" class="table table-bordered"> <tbody> <tr> <th>Text Line</th> </tr> <tr> <td> ...

Leveraging Handlebars for templating in Node.js to incorporate a customized layout

app.js const exphbs = require('express-handlebars'); app.engine('handlebars', exphbs({defaultLayout: 'layout'})); app.set('view engine', 'handlebars'); app.use('/catalog', require('./routes/ ...

Learn how to manipulate data within a MongoDB database schema using Node.js and Mongoose: inserting, saving, and updating records

When inserting data into the MongoDB schema presented below, make sure that Employee name, Project name, and client name can be the same, but the employee ID must be unique. Duplicate entries are not allowed. var StatusSchema = new mongoose.Schema({ ...

Upgrade to Jquery 2.x for improved performance and utilize the latest ajax code enhancements from previous versions

We are encountering a minor issue with Jquery while loading numerous ajax files in our system. Currently, we are using Jquery 2.x and need to be able to operate offline on IE 9+. Previously, when working with Jquery 1.x, we were able to load ajax files fro ...

The exceptional speed of jQuery's each method sets it apart

I'm currently facing an issue where I am unable to successfully add the attribute data-size to my parent div. My code snippet is as follows: var width, height; $(".galerie img").each(function() { width = this.naturalWidth; height = this.naturalH ...

Retrieve a targeted table from a webpage through Ajax refresh

On a webpage, I have a table that has two different views: simple and collapsible. I want to be able to toggle between these views using a button click without the need to refresh the entire page. Below is the code snippet I am currently using: $(&apo ...

Is an Ajax powered loading feature used in transitions between pages?

I recently came across this interesting website: It appears that they have implemented a clever technique where new content is dynamically loaded using AJAX, giving the impression of seamless navigation. Additionally, they have succeeded in hiding the bro ...

Check the document's location if the page contains a class with a specific value

I am encountering an issue while using PHPMailer. I am trying to redirect the page after submitting a form and display a success popup window. After successfully submitting the form, a message is displayed in a popup window (JavaScript adds a class ' ...

Encountering a "Window is undefined" error while trying to load a node_module package within a

I am attempting to incorporate the pickr package (a color picker library) into my nuxt.js application. However, I am encountering an error during import, specifically "window is undefined". Below is the code snippet: <script> import Pickr from &apo ...

Learn how to update a fixed value by adding the content entered into the Input textfield using Material-UI

I made a field using the Input component from material-ui: <Input placeholder="0.00" value={rate} onChange={event => { this.setState({ `obj.rate`, event.target.value }); }} /> Whenever I input a rate into this field, ...