Selecting a specific element from a group of elements sharing the same class using JQuery

Is there a way to target individual elements from a group of elements that share the same classes or other properties without assigning different classes to each element? I need to be able to work on each element separately. I have attempted this approach, but it ends up targeting all elements with input:text due to my incorrect logic in selecting each element individually.

var selector = $('input:radio').prop("checked", true);
var element = $ ('input:text');

$(selector).on('change', function( event ) {
if(this){
    $(element).prop('disabled', true);
     alert('disable only this element when radio is selected');
}
else{ 
    alert('others input:text not disabled');
    $('input:text').prop("disabled", false);
}
})

Fiddle:

Answer №1

To easily achieve this, utilize the DOM navigation method with the following code:

var selector = $('input:radio').prop("checked", true);
var element = $ ('input:text');

$(selector).on('change', function( event ) {
    var el = $(this).closest('.input-group').find('input:text');
    element.prop('disabled', function(){
        return !el.is(this);
    })
});

Check out this Fiddle for a working example: http://jsfiddle.net/D2RLR/5874/

If dealing with dynamic input, Event delegation is required (learn more here):

//document should be closest static element
$(document).on('change', 'input:radio', function(){
    var el = $(this).closest('.input-group').find('input:text');
    $('input:text').prop('disabled', function(){
        return !el.is(this);
    })
})

Answer №2

I am not completely clear on your objective, but utilizing $(this) can help target the specific element that triggered the event instead of using a selector.

Perhaps this code snippet aligns with what you are aiming for:

$(selector).on('change', function( event ) {
    $("input:text").prop("disabled", true);
    $(this).parent().siblings("input:text").prop("disabled", false);
})

This script disables all input:text elements and then enables the one linked to the selected radio button.

Answer №3

Give this a shot:

$(selector).on('change', function( event ) {
    $('input:text').prop("disabled", false);
    $(this).closest('.input-group').find(element).prop('disabled', true);

})

http://jsfiddle.net/D2RLR/5869/

Answer №4

Why complicate when you can simplify?

Check out this simplified version

$('input:radio').change(function () {
    $('input[type=text]').prop('disabled', false);
    $(this).parent().next('input[type=text]').prop('disabled', 'false');        
})

Answer №5

It seems like there may be some confusion regarding what you are trying to accomplish with your query and how specific or general your question is. If the elements you are selecting have the same attributes, including class, you might need to consider the context they are within or even their order in the DOM.

  • Context: For example, if you are looking for elements with the class "many_like_me" inside a parent element with the ID #parent_id, you can refine your selector as "#parent_id p.many_like_me".

  • Order: Alternatively, if you know you want to select the third element in the DOM that matches your selector, you can use the get() method like so: $("p.many_like_me").get(2) (remembering that the index starts at zero).

If your selection needs to be based on an event triggered by another nearby or related element, then some of the other suggestions provided here may be helpful.

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 IE11 not saving specific object value after performing a GET request

I'm encountering a strange issue where Internet Explorer (IE11) is having difficulty saving a specific variable returned from a GET request. This particular variable works fine in other browsers, but seems to be empty in IE. Below you can see a snipp ...

Error: Jquery Ajax popup form unable to submit data

I am having trouble with my pop-up form as it does not seem to be sending the submitted data to jQuery. Can you help me figure out what I might be doing wrong? Here is the script that creates the popup: <script type="text/javascript"> $(document ...

How can I merge data() objects in jQuery?

I am facing a scenario where I have two separate data objects stored on different nodes in the DOM. For example: $('node1').data('id','1'); $('node2').data('id','1'); I am looking for ways to m ...

Tips for ensuring that hover:after contents do not impact the positioning of the next element

I have created a photo list section on my webpage. <div id="main_post_photo"> <% for(var q=0;q<resolve.length;q++){ if(resolve[q].images.length>=1){%> <a href='/post/<%= resolve[q]._ ...

Retrieving data through the combination of jQuery, PHP, and MySQL

Has anyone encountered issues with specifying the default value when pulling values from MySQL? My goal is to repopulate dropdown lists with $_REQUEST fields for easier editing. $(document).ready(function(){ $("#region").load('getRecords.php?s ...

I'm looking for a method to retrieve the value of an option tag and then pass it to a helper within handlebars. Can someone

Currently, I am utilizing express and handlebars in my project. I have a specific view where I aim to display certain information based on the event when a client selects an option from a select tag. However, I have some queries regarding this: Is it fea ...

Dealing with an undefined variable problem in CodeIgniter

My current issue involves loading photos from the database using a for each loop to display each photo in a formatted div. However, I keep encountering an undefined variable 'imgres' and I am uncertain as to why. Here is the corresponding view c ...

How can one effectively compare the value received from value.val() with a regular expression in search fields? Is it possible to use multiple

Feeling a bit overwhelmed. I have come up with this code that is working really well: HTML: <p class="no-result"> Nothing found. </p> JS: (JQuery) $("#itemSearch").on("keyup", function () { if ($("#itemSearch").val() == '') { ...

Execute an AJAX request when the browser is closed, rather than when the URL is

Is there a way to automatically log out a user when they close the browser without manually logging out of the application? I am looking for a solution using jQuery, AJAX, and PHP. Here is the code snippet I tried: <script type="text/javascript> w ...

I am currently facing difficulty in retrieving the selected value from a jQuery dropdown menu

I have a project that involves currency conversion, where I need the input value and selected dropdown value to update dynamically on the same page. There are two input tags and two select dropdown tags for this purpose. $(document).ready(function () { ...

The Dysfunction of JQuery UI 1.12.1 Autocomplete

I am encountering some challenges with implementing JQuery UI. The version of JQuery UI I am using is 1.12.1, the latest one available. I have downloaded the autocomplete feature along with all other necessary widgets. In my project, I have placed jquery ...

Transferring files to Django using AJAX

I am struggling with the process of uploading files to django using ajax. The upload is done within a modal window. The Form <div class="modal fade bs-example-modal-lg" id="fileUploadModal" role="dialog" aria-hidden="true"> <div class="modal ...

Destructuring and For of Loop in ES6: Capturing the first object only

Working with my react app, I have a specific object called 'topics' that I am looping through: const topics = [ {name: 'Mon', color: 'blue', id: 1}, {name: 'Tue', color: 'red', id: 2}, {name: 'W ...

Changes on services do not affect the Angular component

Currently facing an issue with my Angular assignment where changing an element's value doesn't reflect in the browser, even though the change is logged in the console. The task involves toggling the status of a member from active to inactive and ...

Issue with hamburger icon in Bootstrap navbar following height adjustment of the navbar

Although my navbar was functioning properly with the hamburger menu expanding down as expected, I encountered an issue after adjusting the height of the navbar. The hamburger button still worked and expanded the menu, but it only displayed the first item o ...

How to dynamically update the maximum y-axis value in Vue-Chart.js without having to completely re-render the entire

Currently, I am involved in a project that requires the implementation of various charts from the Vue-Chartjs library. One specific requirement is to dynamically change the maximum value on the Y-axis whenever users apply different filters. To achieve this ...

An increasing number of DOM elements causing slowdown in performance

We developed a web application using asp.net mvc, javascript and jQuery hosted locally. Our approach involved keeping all HTML elements in the DOM object instead of destroying them, opting to hide and show pages as users navigate through the application. ...

Setting the date in materializecss with jQuery: a simple guide

<div class="input-field col s4" style="max-height: 58px;"> <input id="email" name="email" type="email" maxlength="30" class="validate" required length='30' disabled="true"> <label for="email" d ...

How can I efficiently choose specific documents or items from a MongoDB collection when working within a loop?

I'm struggling to find the right approach for my current project. Essentially, I have a MongoDB collection that I am iterating through using forEach, displaying the objects in a table with ejs. My goal is to allow users to click on specific objects an ...

Change the color when hovering over the select box

Using jQuery, my goal is to change the hover color in a select box from its default blue to red. I understand that the hover color of the select input may vary based on the operating system rather than the browser, but I am making progress towards achievin ...