Eliminate specified text from a group of buttons

My task involves a set of buttons that behave in a specific way when clicked. By clicking on a button, the text "selected" is added to its existing value.

If one button is clicked, I want any other buttons with the text "selected" to have this text removed.

The desired functionality can be achieved by iterating through all the buttons within the specified area (all with the class of searchResult) and removing the text "selected" from each button if it is present.

Below is the code snippet for each button:

<input type="button" class="searchResult" onclick="$(\'#usersToSend > *\').css(\'background-color\', \'#fff\');
var oldValue = this.value;
this.value=\'(selected) \' + oldValue; this.style.backgroundColor = \'#51CA3E\';
document.getElementById(\'hiddenUserFlag\').value = \''.$row['id'].'\';"
value="'.$row['username'].'">

Pay attention to this part of the code:

$(\'#usersToSend > *\').css(\'background-color\', \'#fff\');
var oldValue = this.value;
this.value=\'(selected) \' + oldValue;
this.style.backgroundColor = \'#51CA3E\';

Answer №1

To change the text of selected buttons, you can utilize the :contains() selector along with the text() method in the following manner:

$("button").click(function(){
  $("button:contains('selected')").text(function(){
    return this.innerHTML.replace("selected","");
  })
  this.innerHTML+= "selected";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>button-</button>
<button>button-</button>
<button>button-</button>

Update: Please note that the above code will not function correctly for <input> buttons. In such cases, you should use the .filter() and .val() methods as demonstrated below:

$(":input").click(function(){
  $(":input").filter(function(){
    return this.value.indexOf("selected") > 0
  }).val(function(){
    return this.value.replace("selected","");
  })
  this.value+= "selected";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="button-" />
<input type="button" value="button-" />
<input type="button" value="button-" />

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

Guide to aligning form data with Bootstrap

Struggling to align my form data in the center below. I have attempted various online solutions without success. The use of bootstrap and offset is causing issues with the title for the form and the actual form itself. Any suggestions would be highly appre ...

Limit the selection to just one element in a v-for loop in VueJS

I am utilizing Vue v2 My goal is to change only the properties of the selected element. Specifically, when the response is marked after clicking, it should switch to red color with a text that reads 'Unmark'. Conversely, if the button is clicked ...

Wait for another user keypress event in jQuery after a 0.5 second delay

Currently, I am working on developing a live search feature for my website. In order to reduce unnecessary requests and optimize performance, I am looking to implement a simple jQuery solution (while also ensuring that there is back-end flood control in pl ...

What is the process for eliminating the hover effect on a Bootstrap button?

I have customized a Bootstrap button in my stylesheet to make it dark, but it still has a hover effect from Bootstrap that I want to remove. How can I get rid of this hover effect? Take a look at the code snippet below for reference: .btn-dark { min-w ...

Leveraging JavaScript for setting an ASP.net Dropdownlist

I have made several attempts to set the dropdownlist without success. Despite observing the selectedIndex changing appropriately in developer tools, and noticing the selected option node change from false to true, the dropdownlist still displays the first ...

Iterate through a collection of files in an asynchronous manner by leveraging promises and defer statements

I have a specific objective to navigate through a directory of files, perform certain operations on each file, and then produce a JSON object with a portion of the directory. Initially, I managed to achieve this using the synchronous functions in Node&apo ...

When utilizing the Jquery Upload File Plugin, it is important to note that the collection IEnumerable<HttpPostedFileBase> may not include any

I have implemented the jquery upload file plugin to enable multiple file uploads. I have properly included the necessary CSS and JavaScript references for file uploading. However, when I try to save the uploaded files after entering the client name and att ...

When using AngularJS, encountered an issue where a view was not updating with new data from a $http request

When making a request to the 500px API using $http for popular photos, the response object is successfully returned. However, I am facing difficulty in pushing the retrieved photo items to the view. Below is my current controller code: meanApp.controller ...

How can I retrieve the URL of images listed in my CSS file using Python?

I need help extracting all image URLs used in a CSS file. Here is an example snippet from my CSS file: sitewrap { width: 890px; margin: 0 auto; background: #ffffff url(../images/bg_sitewrap.gif) repeat-y; } Can anyone advise on how to extract ...

Create a left-aligned div that spans the entire width of the screen, adjusting its width based on the screen size and positioning it slightly

I have a parent container with two child elements inside. I want the first child to align to the left side and the second child to align to the right side, but not starting from the exact center point. They should be positioned slightly off-center by -100p ...

Occupying room while using display: none in <td>

As a newcomer to media queries and .net, I find myself struggling with the code below. At max-width 775px, my goal is to have the first td disappear completely and be replaced by the second td. However, it seems that while the first td becomes 'invisi ...

Exploring through angular.js with the use of identifiers

In my data, I have two JSON objects: all_users "all_users": { "4":{ "user_id":4, "user_name":"Miranda" }, "7":{ "user_id":7, "user_name":"seconduser" } And tickets "tickets": [{ "ticket_id" : 12, "created_by" : ...

What is the best way to center images horizontally in CSS and HTML?

I'm looking to create a driver's page where the desktop view displays images horizontally instead of vertically. I've tried adjusting the display attribute with limited success, and so far, the grid display is the closest I've come to a ...

Combining synchronous and asynchronous code in JavaScript with Node.js and MongoDB

Check out this code snippet: var re = new RegExp("<a href=\"(news[^?|\"]+).*?>([^<]+)</a>", "g"); var match; while (match = re.exec(body)){ var href = match[1]; var title = match[2]; console.log(href); db.news.fin ...

Discovering if a checkbox is checked using jQuery - a simple guide

Can anyone tell me an easy method to determine if a checkbox is checked using jQuery? Perhaps something similar to .checked or ischecked. ...

What are the steps to utilizing the $document service in AngularJS?

I am a complete beginner when it comes to AngularJS and JavaScript in general, and I find myself a bit confused about the usage of $document. From what I understand, $document provides access to some JQuery functions. So, if I want to remove an element tha ...

Different ways to prevent invalid entries in an input field with type datetime-local

<input type="datetime-local" step="1"> Is there a way to prevent invalid date input? For example, entering a date like "11:11:1111" in the format "mm-dd-yyyy". How can this be addressed using Moment.js? ...

Utilizing AJAX to amplify the worth of plupload's echo capabilities

Greetings! Here is the JavaScript code I am using for plupload var uploader = new plupload.Uploader({ runtimes : 'html5,flash,silverlight,html4', browse_button : 'pickfiles', // you can pass in id... container: document.getElementById( ...

Finding the latitude and longitude coordinates of a point at a specific distance from another using Node.js or JavaScript

My task involves determining the square area based on a latitude and longitude (x,y) as shown in the provided figure. https://i.sstatic.net/Rt7mC.png To accomplish this, I must calculate the coordinates of the remaining three corners by adding 10 kilomet ...

Prevent the automatic inflation of bubbles on the D3 World Map

Currently, I am developing a D3 world map with a zoom feature that allows users to zoom in up to the boundary level of any country or county by clicking on it. I have successfully added bubbles that point to various counties in Kenya, and these bubbles en ...