Using jQuery to modify the content of a value attribute

I need to update the text in a specific element by overriding it with jQuery as I do not have direct access to edit the original text.

While I understand the basics of how this works, I am unsure of the correct method to target and replace the value.

Using an ID like 'demo' makes it easier:

<script>
$(document).ready(function(){
  $('#demo').text('The replaced text.');
});
</script>

Now, my question is regarding changing the text ("Credit Card/Debit Card (Affirm)") within the following code snippet to be "affirm". The catch is that I cannot rely on the payment_method name because it is shared with another payment method:

<label>
<input type="radio" name="payment_method" value="affirm">
Credit Card/Debit Card (Affirm)                            
</label>

Thank you for your help!

Edit 1:

The entire HTML Code

<div id="content" class="col-xs-12 col-sm-12">
            
                <p>Please select your preferred payment method.</p>
            <div class="radio">
            <label>
                                                        <input type="radio" name="payment_method" value="authorizenet" checked="checked">
                                Credit Card/Debit Card (Authorize.Net)                            </label>
        </div>
            <div class="radio">
            <label>
                                    <input type="radio" name="payment_method" value="affirm">
                                Credit Card/Debit Card (Affirm)                            </label>
        </div>
    <p><strong>Add comments about your order.</strong></p>
<p>
    <textarea name="comment" rows="8" class="form-control"></textarea>
</p>
<div class="cart-module">
    <div class="cart-content">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <h4 for="input-coupon">Coupon Code</h4>
                    <div class="input-group">
                        <input type="text" name="coupon" value="" placeholder="Coupon Code" id="input-coupon" class="form-control">
                        <span class="input-group-btn">
                            <input type="button" value="Apply" data-code="coupon" data-loading-text="Loading..." class="btn btn-primary">
                        </span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <h4 for="input-voucher">Gift Certificate Code</h4>
                    <div class="input-group">
                        <input type="text" name="voucher" value="" placeholder="Gift Certificate Code" id="input-voucher" class="form-control">
                        <span class="input-group-btn">
                            <input type="button" value="Apply" data-code="voucher" data-loading-text="Loading..." class="btn btn-primary">
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    <div class="buttons">
        <div class="pull-right">I have read and agree to the <a href="https://hhtruckaccessories.com?route=information%2Finformation%2Fagree&amp;information_id=5" class="agree"><b>Shipping and Returns</b></a>.                            <input type="checkbox" name="agree" value="1">
                        &nbsp;
            <input type="button" value="Continue" id="button-payment-method" data-loading-text="Loading..." class="btn btn-primary">
        </div>
    </div>
<script src="/js/catalog/checkout/payment_method/bundle.js?v=1.58.1" type="text/javascript"></script>        </div>

Answer №1

text() does not work with input, so you need to reconstruct the HTML line

var newtext = "affirm";
selector = $("input[type=radio][value=affirm]").closest("label");
var line = selector.html().split(">")[0] + ">" + newtext;

selector.html(line);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content" class="col-xs-12 col-sm-12">

  <p>Please select your preferred payment method.</p>
  <div class="radio">
    <label>
      <input type="radio" name="payment_method" value="authorizenet" checked="checked">
       Credit Card/Debit Card (Authorize.Net)
    </label>
  </div>
  <div class="radio">
    <label>
      <input type="radio" name="payment_method" value="affirm">
        Credit Card/Debit Card (Affirm)
    </label>
  </div>

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

ASP.NET - Severely struggling with connectivity issues involving AJAX/JQUERY

I have developed a real-time update script that refreshes certain div elements with server-side information every second. The issue I am facing is that the script seems to be causing heavy usage, as even my mouse animation in Google Chrome keeps loading. ...

Is there a way to determine the remaining time or percentage until document.ready is reached?

My usual approach is to display a loading animation like this: $('#load').show(); $(document).ready(function(){ $('#load').hide(); }); where the <div id="load"> contains just an animated gif. However, I've been conside ...

The callback function is not being executed in the Ajax request

$(document).ready(function(){ var requestURL = 'http://www.football-data.org/soccerseasons?callback=?'; $.ajax({ type: 'GET', dataType: 'json', url: requestURL, success: function(data){ cons ...

Effortlessly sending multiple values from text fields using jQuery

i am using jQuery to fetch all values from text fields with the same field name. $('input[name^="StudentName"]').each(function() { StudentName += $(this).val(); alert(StudentName); }); when I alert, all the values are displayed as one s ...

Are you looking to load pictures using jQuery?

I am currently using a ul li menu setup as follows: <ul> <li><a href="#1">Image #1</a></li> <li><a href="#2">Image #2</a></li> <li><a href="#3">Image #3</a></li> ...

"Incorporate multiple data entries into a table with the help of Jquery

How can I store multiple values in a table row using jQuery or JavaScript when the values come from a database via Ajax? <html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th ...

Display the initial three image components on the HTML webpage, then simply click on the "load more" button to reveal the subsequent two elements

I've created a div with the id #myList, which contains 8 sub-divs each with an image. My goal is to initially load the first 3 images and then have the ability to load more when clicking on load more. I attempted to follow this jsfiddle example Bel ...

Having trouble sending the selected value from a dropdown list to the server using $.ajax

I am embarking on my first project using JQuery (jquery-3.0.0.min.js) and JavaScript. My goal is to build a straightforward web form that connects to a database through a Web API. The form consists of various input text fields and two select dropdowns. ...

Update the section tag upon submission using jQuery on a jQuery Mobile HTML page

I have integrated a jquerymobile template into Dreamweaver 6.0 to create a mobile app interface. The home screen features four buttons - specifically, View, Create, Update, Delete. Upon clicking the Create button, a new screen is opened (each screen corres ...

Show a Pop-Up When a Key is Pressed

My website popup features a 'Close' button in the top right corner, a text section with a background image, and a 'Print' button at the bottom. The popup automatically appears when the page loads. However, once closed, there is currentl ...

What are the common causes of server response issues in AJAX?

I have created a Facebook app that utilizes ajax requests and responses every 3 seconds. The app also has menu items that load content in the main div. All ajax requests are directed to a common.php file. However, some ajax requests are slower than others. ...

Looking for suggestions on how to bring this idea to life

I'm searching for a solution using JavaScript, jQuery, or Angular. I've created three random arrays like this: for example: (The values are randomly generated from the array ['member', 'medical', 'rx', 'disabi ...

Delete the content on a webpage using an Ajax jQuery request to transfer it elsewhere

Whenever I submit a form using an ajax post request, I receive values from the controller and manipulate the page based on those values. However, my issue is that I need to erase the values from the previous request when the form is submitted again. For in ...

Achieving CSS Buttons That Change to Green When Clicked and STAY Green

I've been tasked with working on a front-end project for a website that involves buttons changing color when clicked and staying that color. Here is the CSS <style> code I have written for these specific buttons: .button { background-color ...

retrieve index in jquery attribute (grouped)

I currently have six <div class="gallery-popup"> elements, each containing thumbnails within <a href="..."> tags. My goal is to add the attribute data-lightbox="gallery-X" to each <a> element, but with a specific identifier for each of ...

I seem to be stuck trying to figure out what's going wrong when receiving JSON data

I have spent hours researching on Stack Exchange and Google to gather different information. Here is what I have in guess.php: PHP header('Content-type: application/json'); get_rating($cleanMovie); json_encode(array($id)); The get_rating funct ...

Downloading a PDF file received from a Django view using JavaScript and jQuery

I have a celery task that creates PDF files on the click of a button. When the button is clicked, the JavaScript side will keep checking until the task is done, and when it is, it will download the file. Some recursion should do the trick: $(".getPdf").o ...

Animate the chosen text via specialized effects

I am trying to implement a show/hide feature for specific text using jQuery. The challenge I am facing is having multiple instances of the same text tag with identical ids. I want to trigger the animation on a particular child element when hovering over it ...

Retrieving data with jSON through the local storage API

Seeking assistance with a problem I'm facing. Being new to this, the textbook Headfirst into programming isn't very helpful in explaining. After researching on stackoverflows, I'm still struggling. Any guidance would be greatly appreciated. ...

Issue with function incorrectly computing values and returning NaN

My challenge is to develop a countdown timer, but it keeps returning NaN instead of counting down. I have multiple counters in place - one that counts down, another that counts up until the stop time specified by stoptime, and a third that kicks in after s ...