What is the best way to retrieve the rel value using jQuery?

In my process, I utilize jQuery UI drag and drop functions to pick out images from an album. The images' ID is stored in the "rel" parameter, which I then need to transfer to an input element after dropping the image.

Below is a list of images that can be dragged:

<img src="01.jpg" title="image 01" rel="10" />
<img src="02.jpg" title="image 02" rel="11" />
<img src="03.jpg" title="image 03" rel="12" />

Drop the image here:

<div id="dropzone">
    <div id="imgcontainer"></div>
    <input type="text" name="img_id" id="img_id" />
</div>

The droppable JavaScript code is provided below:

$('#dropzone').droppable({
    hoverClass: 'bgSelected',
    drop: function(event, ui) {
        $(this).html(''); // removing any other elements
        var img = ui.draggable;
        $('<img src="'+img.prop('src')+'" title="'+img.prop('title')+'">').appendTo( $(this) );
        $('<input type="text" name="img_id" id="img_id" value="'+img.prop('rel')+'" />').appendTo( $(this) );
    },
});

However, there seems to be an issue with retrieving the "rel" value using img.prop('rel'), as it returns an empty string. I have also attempted img.attr('rel'), but that seems to return an object element instead. What would be the correct way to extract the "rel" value?

Answer №1

instead of using prop, you can utilize the attr method. for example ..

$(function() {
  $('#dropzone').droppable({
    hoverClass: 'bgSelected',
    drop: function(event, ui) {
         var img = ui.draggable;
        //alert($(ui.draggable));
        var id =img.attr('rel');


    $(this).html(''); // to remove any other stuffs

     $('<img src="'+img.prop('src')+'" title="'+img.prop('title')+'">').appendTo( $(this) );
      $('<input type="text" name="img_id" id="img_id" value="'+id+'" />').appendTo( $(this) );
    },
  });

check out the fiddle link for this code snippet http://jsfiddle.net/fLJuB/19/

Answer №2

console.log($("#image").attr("data"));

Trying to use .attr("data") instead of .text() might not give the desired result.

Answer №3

To access other properties in the object using ui.draggable, consider utilizing jQuery selectors. For instance, if img.prop('src') successfully retrieves the src attribute, you can utilize it to target the image using a jQuery attribute selector.

alert($('img[src="' + img.prop('src') + '"]').attr('rel'));

UPDATED: I believe this solution should work well -> link

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

Click the "Add" button to dynamically generate textboxes and copy the contents from each

I am working on a project where I have an Add button and 6 columns. Clicking on the Add button generates rows dynamically, which can also be deleted. My challenge is to copy the content of one textbox into another in 2 of the columns. This copying function ...

Retrieve the HTML tags following the modification of my information in ASP.NET

Apologies for any language errors, I am new to asp.net development. 1- I have a table with dynamically created rows and columns based on user selection of row and column numbers. 2- Each row in the table has the following controls: A- One textbox, one l ...

Including a few personalized Jquery Functions in an external Script

I am looking for assistance in moving the following two functions into an external file. $.fn.clearSelect = function () { return this.each(function () { if (this.tagName == 'SELECT') this.options.length = 0; ...

How to interrupt a JQuery animation and restart it from the middle?

Currently, I am tackling my first JQuery project and facing a challenge. As the user mouseleaves the .container, the animation does not reset but continues as if they are still within it. My goal is to have the animation revert in reverse if the user decid ...

In Internet Explorer 7, there appears to be a white box that covers up the content on our website beyond a

I'm feeling stuck and frustrated trying to solve this issue. I have a Mac and no access to an IE7 machine, so I've been using browserlab (which is slow) to make any changes and see if they help. Unfortunately, I haven't been able to fix the ...

The delete button is designed to remove the record from the database while keeping it visible on the frontend

When the delete button is pressed, it successfully removes the record from the database. However, it does not disappear from the frontend until the page is reloaded or refreshed. This is what I see: @foreach (var item in Model) { <a href="#" ...

Troubleshooting: jQuery addClass() function not functioning properly in conjunction with attr("href", something)

I am trying to implement a unique feature for a link using a Bootstrap button, where it only works on the second click. On the first click, the appearance of the link should change slightly. To achieve this, I am utilizing the .addClass(newClass), .removeC ...

The AJAX request was a success, but unfortunately there is no usable data available

After submitting the registration form, jQuery sends the data to register.php, which then responds with JSON data for jQuery to process. Everything seems to be functioning correctly as users are successfully registered. The network response tab shows that ...

Eliminating the 'white-space' surrounding concealed images

I am currently working on a project where I have a list of images that need to be hidden or shown based on the click event of specific <li> elements. While I have managed to achieve this functionality successfully, I am facing an issue with white spa ...

Ways to apply a box shadow exclusively to specific elements

Is there a way to apply a box-shadow to a div without it affecting specific other divs? In simpler terms, is it possible to prevent shadows from being cast on a particular div? Take for example this scenario: http://jsfiddle.net/Cd6fE/ How can I prevent ...

Working condition may vary depending on the size of the item

I have integrated the Masonry jQuery plugin into my design, but it is not functioning properly. The CSS class mentioned in the documentation only includes: width: 25% For my purposes, I modified it to: width: 24% float: left margin-right: 5px This modi ...

Looking for a versatile jQuery plugin that enables right-click functionality across different browsers?

I am currently using a context menu plugin that displays a context menu when an element is right-clicked. However, this does not work on elements embedded with ajax. To solve this issue, I am considering utilizing ajax live to trigger the context menu func ...

Comparison of jQueryUI and Bootstrap button styles

Currently, I am utilizing the following libraries: bootstrap.min, jquery-1.10.2, jquery-ui-1.10.4.min. My goal is to implement a drag and drop feature using these libraries. However, I have encountered an issue where Bootstrap buttons are not draggable des ...

How can you determine if multiple checkboxes have been checked with "if" statements following a button click?

I am looking to display a message after clicking a button if one or more checkboxes are checked. I would prefer using Jquery for this functionality. Any help on achieving this would be highly appreciated. $(function() { $(".btn").click(function() { ...

Access the original source code using jQuery

Is there a way to retrieve the raw HTML code (as seen in Chrome's source code window) using JQuery without having quotes converted to &quot or HTML symbols converted to text? I have tried using html() and text(), but neither method seemed to give ...

An additional "?" symbol found in the PHP file at the top left corner

I am experiencing an issue with a PHP page where a "?>" symbol appears in the top left corner. The code snippet looks like this: <?php include_once("DBconnect.php"); $getuser = $_POST["RegUsername"]; $getpass = $_POST["Pass"]; $getrepass = $_POST["ReP ...

Troubleshooting: JQuery dropdown selection not updating image display

I am trying to create a select menu that changes the car image when a user selects a different model, but for some reason it is not working. Here is what I have tried: <h2 class="model">A6 <img src="images/3.jpg" id="image" width="544" height="2 ...

Issues with Thunderbird not displaying copied HTML emails

Hello there amazing people at Stackoverflow. I need some assistance with HTML formatting. I am currently working on a bootstrap modal that is being dynamically modified through jQuery using the append() function. Check out the code snippet below: <div ...

Using Jquery to continuously animate elements breaks and repeats the process

I am currently using a jQuery function to animate my text. The issue is that it animates all elements simultaneously instead of separately. I would like each element (h2, h3, and span) to be animated individually. Any suggestions on how I can achieve this ...

How can I display a variable within a parent div span and a child div span?

Let's consider a variable, let's call it item. This variable is an array that holds two values: name and size. For example: item[0]=name; item[1]=size; Now, I want to display the name in a span inside the parent div and the size in a span insid ...