How to retrieve the image source from a block using jQuery

Currently, I am working on developing a slider for my webpage using jquery's "Cycle" plugin. However, I have encountered an issue with accessing the image sources used in the slider. To provide more context, here is a snippet of code from the 'head' section:

<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jquery.cycle.all.js"></script>
<script type="text/javascript">
    $('#slider').before('<ul id="pager">').cycle({ 
        fx: 'scrollHorz',
        speed: 900,
        timeout: 5500,
        pause: 1,
        pager: '#pager',

        pagerAnchorBuilder: function(idx, slide) { 
        return '<li><a href="#"><img src="' + slide.src + '" width="120" height="65" /></a></li>'; 
        } 
    });
</script>

Additionally, here is a portion of the html content related to this problem:

<div id="slider_container">
    <div id="slider">
        <div class="items">
            <img src="1.jpg"/>
            <div class="info">
                <h2>Tile Nr. 1</h2>
            </div>
        </div>
        <div class="items">                         
            <img src="2.jpg"/>
            <div class="info">
                <h2>Tilte Nr. 2</h2>
            </div>
        </div>
    </div>
    <div id="pager">

    </div>
</div>

Within the slider division, I aim to display blocks comprising both images and corresponding titles. Conversely, within the pager segment, only the images utilized in the slider should be shown. The stumbling block lies in correctly retrieving the image source from the relevant items block using the slide.src expression in the third JavaScript. How can I modify it to access the intended image sources?

Answer №1

Could you please create a jsFiddle that includes the necessary jquery files?

I believe that using slide.src may not be the correct approach. If slide refers to an element in the DOM, you should access its 'source' attribute like this: $(slide).attr('src')

Update:

It turns out that 'slide' refers to the containing div, so we should use the following instead:

$(slide).find('img').attr('src')

Answer №2

Upon examining the parameter slide within the pagerAnchorBuilder function, you will notice that it is a div element with the class items, and it contains an img element as its first child. This allows you to access the img tag and its src attribute in the following manner:

pagerAnchorBuilder: function(idx, slide) {
    var source = $('img', slide).attr('src');
    return '<li><a href="#"><img src="' + source + '" width="120" height="65" /></a></li>'; 
}

There are alternative ways to achieve the same result:

var source = $(slide).find('img').attr('src');
// or using native JavaScript
var source = slide.querySelector('img').getAttribute('src');
var source = slide.querySelector('img').src;
var source = slide.firstElementChild.src

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

Tips for selecting multiple potions from a JSON file in a React Native project

I need help with highlighting multiple options from an array in React Native. Currently, when I click on an option, it highlights that option but de-highlights the previous one. How can I modify my code to allow for selecting and highlighting multiple opti ...

Creating a User-friendly Layout for Admin Pages in Next.js Version 13

Hey there, I'm facing an issue with the layout while using Next.js 13 Experimental App Directory. On my website's index page or routes '/', I want to show a landing page and use a specific layout for all pages except for those under the ...

Setting the current date of a jQuery datepicker to the value of an input tag

I am looking to assign a value to an input tag that has a datepicker attached to it. Upon initialization of the datepicker, I want to set this value. Below is a hypothetical example of what I am trying to achieve: HTML: <input id="test" value="">&l ...

MUI Datepicker options for selecting dates

Is there a way to selectively enable certain dates and disable all others in the MUI Datepicker? For example, I need to only allow dates that are 7 days later or earlier to be selectable, while all other dates should be disabled. How can this be achieved? ...

Unspecified data output from jquery datepicker

I am currently utilizing a datepicker to select a date. I have implemented the jQuery datepicker, however, it is returning an undefined value. Can someone please assist me with this issue? My HTML CODE: <input type="text" class="form-control fromdate" ...

Issue with rendering Base64 image array strings in FlatList component in React Native

In my RN App, I am trying to display a FlatList with Image Items but it seems like I have missed something. I am retrieving blob data from my API, converting it to a String using Buffer, and then adding it to an Array. This Array is used to populate the F ...

Click the button to reset all selected options

<form method="post" action="asdasd" class="custom" id="search"> <select name="sel1" id="sel1"> <option value="all">all</option> <option value="val1">val1</option> <option value="val2" selected="selected"> ...

The reason for setting a variable as void 0 in JavaScript

Currently, I am delving into the libraries referenced in this particular article as well as other sources. There are some truly mind-boggling concepts contained within these resources, one of which is highlighted by the following line: var cb = void 0; I ...

The tab content in VerticalTab Material React UI is spilling out and overflowing

Having some trouble with this particular component. Whenever I input content larger than my Grid size, it overflows the VerticalTab section. You can see an example of this in this codesandbox. Here's what I've attempted: return ( <div cla ...

Is your jQuery .on function failing to properly detect click events?

Seems like I'm missing something here. Why is the .on function not functioning as expected in this code snippet? <html> <head> </head> <body> <button type="button" class="close" data-test="test">TEST BUTTON< ...

Display a tooltip for ever-changing content

My HTML code displays dynamic rows with information, along with an image link that reveals specific details about the clicked row using the compentence_ID field. echo "<td>".$compi['Competence_ID']."</td>"; ec ...

Certain CSS styles for components are missing from the current build

After building my Vue/Nuxt app, I noticed that certain component styles are not being applied. In the DEVELOPMENT environment, the styles appear as expected. However, once deployed, they seem to disappear. All other component styles render properly. Dev ...

Looking to incorporate Slick Carousel as the main slider on my website for a dynamic hero section using JavaScript

Currently in the process of expanding my knowledge in javascript, I am now delving into setting up Slick Carousel. It is essential that it functions as a hero slider on my website. If you are interested in checking out Slick Carousel on github, feel free ...

What sets apart using "!=" from "=" in the Jade template engine when assigning a variable?

I am a beginner in NodeJS and JS, so please excuse me if this question seems simple. Recently, I came across an example in the ExpressJS documentation that showed how to utilize the Jade template engine. The example included the following lines: html h ...

An error occurred when attempting to run the command npm run compile:sass, displaying the message: npm ERR! missing script:

Everything seems to be in place with the sass folders and files, so what could be the issue? I have my package.json file set up correctly with the following code: { "name": "starter", "version": "1.0.0", " ...

Deactivating a form field depending on a selected radio button in Angular 2

If I have two radio buttons, with a click function called localClick for the first button to give value 1 and the second button to give value 2. <div class="ui-g-12"><p-radioButton name="group1" value="Local" (click)=localClick(1) label="Local"&g ...

What is the best way to manage a situation where a web element is removed from the DOM while using Selenium Web

On the Sign In page of , I am struggling to extract the value of a label identified by the xpath=".//*[@id='msgIdmmrka']." This particular web element disappears from the DOM a few seconds after entering no email address, a valid password, and cl ...

Is there a way to align my horizontal menu with the top of the screen without encountering the hover problem?

I'm trying to make my horizontal navigation bar stay at the top of the screen. The issue is that when I set the top property to 0, the menu sticks to the top but the initial menu item is obscured when hovered over. If I remove the top property, the ho ...

Refresh a div automatically with a new DataFrame every few seconds

I've searched extensively online for solutions, but I'm struggling to find a simple and efficient way to reload just the dataframe in an HTML table on a Flask page every few seconds without refreshing the entire page. app.py from flask import F ...

jQuery Autocomplete API Issue: Undefined

I've been attempting to implement a basic text box using the jQuery API from DevBridge. I followed instructions in this video tutorial to create my code. However, despite properly declaring scripts before the JS code and ensuring proper mappings, I&a ...