Exploring the capabilities of this class in Jquery attribute manipulation

Presented below is a piece of code written in HTML and jQuery

HTML

<div id="myForm" class="myForm row">
    <div class="form-group row">
        <div class="col-lg-12">
            <div class="col-md-4">
                <label>Name of Friend *</label>
                <input type='text' name='name_friend[]' id='name_friend[]' class='form-control name_friend[]' placeholder='Name' required />
            </div>
            <div class="col-md-4">
                <label>Phone Number*</label>
                <input type='text' name='nomor_telepon[]' id='nomor_telepon[]' class='form-control nomor_telepon[] telepon phone-email' placeholder='Phone Number' required />
            </div>
            <div class="col-md-4">
                <label>Email</label>
                <input type='email' name='email_friend[]' id='email_friend[]' class='form-control email_friend[] phone-email' placeholder='Email' />
            </div>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-lg-12">
            <div class="col-md-4">
                <label>Name of Friend *</label>
                <input type='text' name='name_friend[]' id='name_friend[]' class='form-control name_friend[]' placeholder='Name' required />
            </div>
            <div class="col-md-4">
                <label>Phone Number *</label>
                <input type='text' name='nomor_telepon[]' id='nomor_telepon[]' class='form-control nomor_telepon[] telepon phone-email' placeholder='Phone Number' required />
            </div>
            <div class="col-md-4">
                <label>Email</label>
                <input type='email' name='email_friend[]' id='email_friend[]' class='form-control email_friend[] phone-email' placeholder='Email' />
            </div>
        </div>
    </div>
</div>

jQuery

$(".phone-email").blur(function(e) {
    var param = $(this).val();
    var url = "<?php echo base_url('/Campaign/check_data');?>";
    $.ajax({
        url: url,
        type: "POST",
        data: {
            param: param
        }
    }).done(function(d) {
        var obj = JSON.parse(d);
        if (obj.status == "0") {
            alert(obj.message);
            $("#add-friend").attr('disabled', 'disabled');
            $(this).attr('style', 'border: 2px solid #a94442');
        } else if (obj.status == "1") {
            $("#add-friend").removeAttr('disabled');
        }
    });
});

The issue with this code is that the element's class does not have a border as intended. The goal is to add a red border to the input when status = 1, but it does not work correctly.

$(this).attr('style', 'border: 2px solid #a94442');

Answer №1

Your current implementation lacks the proper usage of the this element context within the .ajax() method when dealing with the phone-email element.

Try this approach:

$(".phone-email").blur(function (e) {
    //Save the current context in a variable
    var _this = $(this); 

    $.ajax().done(function(d){
        //Utilize the saved variable
        _this.attr('style', 'border: 2px solid #a94442');

        //Alternatively, you can use .css() to modify CSS properties
        //_this.css('border', '2px solid #a94442');
    }); 
}); 

Disclaimer: Only essential parts of the code have been retained for clarity.

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

Margin in Bootstrap Panel Not Set to 0 as Needed

My bootstrap panel's margins are set to 0, but it doesn't seem to actually be 0. In the image attached, the margin shows as "-", which I assume means 0. However, when I highlight it, I see a large block of orange. Does this indicate that the marg ...

How to retrieve values from dynamically generated text boxes using ng-repeat in AngularJS

When using ng-repeat, I am able to display textboxes related to each item. Upon clicking the SaveAll button, I intend to retrieve all textbox values based on ITEM ID and save them to the database. <tr> <td> <table ng-repeat="item in ...

Identifying differences in a Knockout view model

While it may seem like a simple question, is there actually a straightforward method to identify if there has been a change in any property of a knockout view model? ...

What is the way to display the final list item when clicking in jQuery?

I am attempting to achieve a specific behavior where clicking on a button will trigger the content below to scroll in such a way that only the last item in the list is visible. I have been using jQuery for this functionality, but unfortunately, it is not ...

Enhancing Win8 apps with AppendTo/jquery-win8

Recently, I've been intrigued by the ToDoMVC samples and decided to try porting them into a Windows 8 JS app. I thought it would be as simple as copying and pasting the code while making sure to reference the necessary WinJS libraries. However, I soo ...

The 'controllerLogin' argument is undefined and not a function in AngularJs, causing an error during Karma Testing

Hello Brilliant Developers, I'm currently diving into the world of AngularJs and running into some bumps with basic testing, resulting in pesky errors. I've scoured the web for solutions to no avail. Any assistance you can provide would be great ...

Adjust the height automatically in extjs hbox layout with this quick guide

When I try to dynamically set data in this layout, the layout doesn't resize properly and the final result looks like this. This is the code I'm currently using: win = Ext.create('widget.window', { title: 'Layout Window&apo ...

Troubleshooting custom fonts not displaying on a .NET website: Importing fonts for web use

https://i.stack.imgur.com/uWSsY.pngI am well-versed in HTML, but my knowledge of VB.NET is limited. In standard CSS, including a font like the one below would typically work, but I have encountered issues when attempting to do so in .NET. @font-face { f ...

Utilize the withCredentials property in conjunction with $resource

I am looking to utilize a resource with a cookie set in the navigator. Using $http makes it simple, just need to set withCredentials to true: $http({ method: 'POST', url: url, data: user, withCredentials: true }); However, for ...

In what situations is it essential to utilize the `rerender` function in the React Testing Library?

In the past, my team and I usually focused on writing React Testing Library (RTL) tests for the main parent components that contained numerous nested child components. This approach made sense and proved to be effective. The child components in question we ...

Refreshing the jQuery function following an ajax request

At the moment, I am using a jQuery plugin that adds a GreyScale hover effect to my website. The plugin is functioning properly and creating the desired visual impact. However, there is a small issue when it comes to utilizing ajax-based filtering on the p ...

The `process` variable is not recognized in a Vue/TypeScript component

I've encountered an issue trying to use .env variables in my Vue app. When I ran npm install process, the only syntax that didn't result in an error when importing was: import * as process from 'process'; Prior to this, I received the ...

What is the best way to check if an email address already exists in a PHP MySQL database?

Challenge <?php mysql_connect('localhost','root','123456') or die(mysql_error()); mysql_select_db('email') or die(mysql_error()); ?> <?php if(isset($_POST['submit'])) { extract($_ ...

Struggling to decipher JSON using jQuery proved to be quite challenging

JSON object (parameters) "selectedShopeNumber":1765653589, "shopeNumbersForSelectedNames":[], "shopeNumbers":[1765653589, 660791222],"shopeNames":["Shope 1","Shope 2"] code var params = JSON.parse("[" + parameters + "]"); for (var i = 0; i < params.s ...

Limiting Error Messages to a Single Failed Promise Chain in Ajax

By chaining my promises in the following manner, var promise1 = step1(), promise2 = promise1.then(function(data) { return step2(); }), promise3 = promise2.then(function(data) { return step3(); ...

Delete the float attribute from the image when the inline-block is shifted to the bottom of the image

I have three elements in my design - a title, a paragraph, and an image. I want the image to float to the right, with the title and paragraph floating to the left and bottom, resembling "Image 1." To prevent the title from wrapping when narrowing the oute ...

What can be done to resolve the error message "Matchong query does not exist"?

Recently, I've been working on developing a like system similar to Instagram's using Django and Ajax. However, after completing the code, I encountered a frustrating error message that read "Post matching query does not exist." Despite thoroughly ...

Converting nested JSON structures from one format to another using JavaScript

I have been attempting to convert an AVRO schema into an ElasticSearch index template. Both schemas are in JSON format with some specific considerations to keep in mind during the transformation process. I initially tried using recursion to extract all nes ...

Having trouble with validation messages not displaying correctly in Angular after removing a value. What is the correct approach to fix this issue

I've encountered an issue with Angular validation where it's triggering validation on page load, which is not desired. Additionally, when I enter a value, the validation message disappears, but if I return to the textbox and delete the input, the ...

Having difficulty importing SVG files into the canvas

I'm encountering difficulties when attempting to import SVGs to the canvas and use setZoom() with FabricJS. I am currently working with version "2.0.0.rc4". I have made attempts to import them using two different methods, each with its own set of cha ...