Refresh the page on the same tab after making an AJAX request

HTML:

<table border="1">
    <tr id="main_account">
        <td width="50%">
            <h3 style="margin-left:10px">Details</h3> 
            <table width="270">
                <tr>
                    <td>First name:</td>
                    <td><input type="text" name="first_name" id="id_first_name" /></td>
                </tr>
                <tr>
                    <td>Last name:</td>
                    <td><input type="text" name="last_name" id="id_last_name" /></td>
                </tr>
                <!-- other html -->
            </table>
        </td>
    </tr>
    <tr id="authorised_reporter">
        <td  width="100%" colspan="2">                            
            <form method="post" action="." id="reporter-form">                                    
                <table  width="100%">
                    <tr>
                        <td style="width:100px;">First name:</td>
                        <td><input type="text" name="first_name" id="id_first_name" /> </td>
                    </tr>
                    <tr>
                        <td>Last name:</td>
                        <td><input type="text" name="last_name" id="id_last_name" /> </td>
                    </tr>
                    <tr>
                        <td>Daytime phone:</td>
                        <td><input id="id_phone_daytime" type="text" class="trim-space" name="phone_daytime" /></td>
                    </tr>
                </table>
            </form>
        </td>
    </tr> 
</table> 

When the page loads, #main_account is visible and #authorised_reporter is hidden. Clicking on #authorised_reporter makes it display block and #main_account display none. After updating user data in #authorised_reporter tab using AJAX and reloading the page, I want to show the #authorised_reporter tab instead of #main_account.

Attempted code that didn't work as desired:

//other code not shown
success: function() {  
    location.reload();
    $('#authorised_reporter').css("display", "block");
    $('#main_account').css("display", "none");
    $("#sucess").show();
    $("#sucess").text("Changes has been updated."); 
}    

Answer №1

The modifications you made may not take effect because...

window.location.reload();

Refreshing the page will prevent the success function from executing its later part. Providing more specific HTML code will allow for a better answer. You can also consider using .done() in jQuery for better control.

Answer №2

Test it out with the following:

Illustration: 1

$("#main_account").hide();  

If this solution does not apply to your situation, feel free to ignore it.


Illustration:2

To ensure a smooth transition in case of degradation, you can continue using jQuery to hide the edit <tr> rows by assigning them a class, for example <tr class="edit"> and then hiding all of them simultaneously outside the table with jQuery like this:

<script type="text/javascript">
$(function() {
    $("tr #main_account").hide();
});
</script>

This method, as opposed to initially hiding them with CSS, is more considerate towards users who have disabled JavaScript. If there is a possibility of such audience, opt for this .hide() approach.

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

What is the level of visibility in Nextjs?

Is it safe to expose the sources of files located in the 'pages/' directory? For instance, if you set up a page specifically for administrators at pages/admin and restrict access through Middleware, does this enhance security measures? ...

The ng-if condition is never met

I am currently utilizing AngularJS in my project. <div ng-repeat="l in kites"> <a ng-click="findit(l.green.num)">color</a> <span ng-if="c_{{l.green.num}} == 'y'>kites is </span> </div> Within my c ...

Repeatedly view the identical file on HTML

I'm currently working with the following HTML code: <input type="file" style="display: none" #file(change)="processImage($event)" /> <button type="button" class="btn" (click)="file.click()" Browse </button> When I select image1 fr ...

My PUT request is encountering difficulties with axios and not being successfully processed

Using the MERN stack, I am developing a basic website that includes CRUD operations. However, whenever I attempt to update the documents, the following logs are generated: name 1 email 1 mem phone 1jnk bundle.js:1014:13 XMLHttpRequest { readyState: 4, time ...

invoking both componentDidMount and componentDidUpdate within the identical code block

componentLifeCycleMethod() { let data ; axios.get('http://localhost:8000/wel/') .then(res => { data = res.data; this.setState( { details: data }); }) .catch(err => {}) } I am looking to opt ...

Ways to distinguish XmlHttpRequest Access-Control-Allow-Origin issues from regular network errors

When making an ajax request, there is a possibility of encountering an error, indicating a failure to establish communication with the intended target (no status code returned). To handle these errors, you can use the following code: var oXhr = new XMLHt ...

Prevent multiple submissions by disabling the Submit button in Magento after it has been clicked and

I am looking for a solution to disable the submit button or display a loading image over the form once the submission has been validated. I have tried various methods to disable the submit button after it is clicked, but these do not account for validation ...

Exploring the interplay between jQuery functions and the "Class" method in Javascript

Is there a way to reference a parent "class" method in Javascript from a jQuery function? Here's an example scenario: $Object.prototype.TestFunction = function(arg){ alert(arg); } $Object.prototype.foo = function(){ $(something).click(function ...

The content within the iframe is not displayed

I've set up a dropdown menu with separate iframes for each option. Here's the code I used: $(function(){ $('#klanten-lijst').on('change',function(){ $('#klanten div').hide(); $('.klant-'+t ...

Modify data in an array using Vuex

When working with my Vuex mutation, I am trying to replace an element in an array within the state. The code snippet below illustrates what I am attempting to do: UPDATE_MAILING(state, mailing) { let index = _.findIndex(state.mailings, {id: mailing.id ...

Include numerous values within a hyperlink

Is there a way to pass three values through a link without displaying them in the URL? ...

Embed JSON data into the text box

I've implemented a function to add a new patient to the database, retrieve the JSON data, and then add the new patient to the select menu successfully. However, I'm facing an issue when trying to insert this data into the relevant text inputs suc ...

"Enhance your visuals with a rotating light source in combination with a camera and Orbit

In my Three.js scene, I've integrated OrbitControls.js for rotation and panning functionality. However, I'm facing an issue with the lighting setup - I want the lighting to move along with the camera, ensuring that the object is always well-illum ...

unable to retrieve the city name through geographical location information

I am currently using the following code snippet to retrieve the country and city names: <?php $user_ip= getenv('REMOTE_ADDR'); $geo= unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip")); $city= $geo["ge ...

Find out if two HTML elements are distinct without using their ID attributes

Is there a way to detect if two references to the same HTML element exist without the ID property being set? For example, with checkboxes declared as follows: <input type="checkbox" class="RowSelector" /> In the code snippet below, the goal is to c ...

jQuery DataTable error: Attempted to set property 'destroy' on an undefined object

<script> $('#archiveTable').DataTable({}); </script> <table id="archiveTable" datatable="ng" class="table table-sm" style="color:black"> <!--some code--> </table> This is a snippet of HTML code Upon checking t ...

Angular 2 isn't triggering the custom validator for every keypress

I have implemented a custom validator for Reactive forms in Angular 2. However, I am facing an issue where my function is only validating the first key press in the text field. Ideally, the custom function should validate each key press. Can someone please ...

Can inner function calls be mimicked?

Consider this scenario where a module is defined as follows: // utils.ts function innerFunction() { return 28; } function testing() { return innerFunction(); } export {testing} To write a unit test for the testing function and mock the return value ...

Unexpected value assigned to private variable within a PHP class

Initially, the issue I am encountering originates from my PHP class that is called by a PHP file accessed through an AJAX call. The main problem lies in the fact that the return value does not align with the sybase_result value. What could possibly be mis ...

How come the centering of a text input field in an HTML form is proving to be so difficult using the text-align: center property?

Learning HTML has been quite the journey for me. One challenge I have faced is trying to center a search bar on my web page. Despite hours of searching and trying different tutorials, I still haven't been able to make it work. The CSS rule text-align ...