Hướng dẫn onclick reload div javascript - onclick tải lại div javascript

I have a script inside a div which I want to refresh when someone clicks a button with a unique ID.

I have some data which will be displayed inside the div - which I need to refresh without refreshing the whole page.

I've found a solution which changes a border colour but I can't quite simplify it to what I need. Here's what I have so far

var storeColor = 'red';

$['#container'].on['click', 'button', function[] {
  var $p = $['#content p'];
  var tmp = $p.css['border-color'];
  $p.css['border-color', storeColor];
  storeColor = tmp;
}];
Refresh DIV
Contents of div

asked Jul 31 at 8:02

I think there are a few problems with your code... The button is outside of the container and thats the reason the click listener doesn't work. Inside the click function you try to access a paragraph p inside some element with the id


  Refresh DIV



Contents of div
0 which doesn't exist in your code

var storeColor = 'red';

$['#btnRefresh'].on['click', function[] {
  var $p = $['#container'];
  $p.css['background-color', 'red'];
  $p.html['hello world'];
}];
  Refresh DIV



Contents of div

answered Jul 31 at 8:12

MichaelMichael

1,9172 gold badges7 silver badges21 bronze badges

Why not try changing the entire inner content of the div dynamically using document.querySelector['.div'].innerHTML =


  Refresh DIV



Contents of div
1

The above will not refresh the page if used upon a button click. You may want to use the preventDefault in the button's event listener too.

answered Jul 31 at 8:17

1

I just wat to know how I can reload a specific element or div with javascript code.

Nội dung chính

div:

hope you will help . I really need it . Thank you

sam

1,55711 silver badges14 bronze badges11 silver badges14 bronze badges

asked Nov 5, 2021 at 13:15Nov 5, 2021 at 13:15

8

You can easily set the source of an image in JS:


  Refresh DIV



Contents of div
2

function refresh[] {
document.getElementById["holla"].src = "avatar.gif";
}
Refresh Image

answered Nov 5, 2021 at 13:20Nov 5, 2021 at 13:20

11

If you are trying to reload from server then you can do ajax call and set the response to that div. document.getElementById['holla'].innerHTML = this.responseText;

answered Nov 5, 2021 at 13:24Nov 5, 2021 at 13:24

1

You must have some mechanism to load the image. Ajax call will load the image asynchronously.

// Create an XMLHttpRequest object
const xhttp = new XMLHttpRequest[];
// Define a callback function
xhttp.onload = function[] {
  //you get response back from server in responseText.
  document.getElementById['holla'].innerHTML = this.responseText;
}
// Send a request
xhttp.open["GET", "ajax_info.txt"];// path to file
xhttp.send[];

answered Nov 5, 2021 at 13:44Nov 5, 2021 at 13:44

Hướng dẫn cách tải lại trang bằng JavaScript. Bạn sẽ biết cách sử dụng thuộc tính location.reload để load lại trang bằng JavaScript sau bài học này.tải lại trang bằng JavaScript. Bạn sẽ biết cách sử dụng thuộc tính location.reload để load lại trang bằng JavaScript sau bài học này.

location.reload trong JavaScript là gì

Tải lại trang bằng location.reload trong JavaScript phương thức location.reload với mã HTML sau đây: là một phương thức của window.location Object, có tác dụng tải [load] lại trang hiện tại.

Tổng kết

hope you will help . I really need it . Thank you

sam

Tải lại trang bằng location.reload trong JavaScript phương thức location.reload với mã HTML sau đây:





Sample



Sample

Ví dụ tải lại trang




let d = new Date[];
let element = document.getElementById['msg'];
element.insertAdjacentHTML['afterend', '

' + d.toTimeString[] + '

'
];


let btnReload = document.getElementById['btnReload'];
btnReload.addEventListener['click', function[]{
location.reload[];
}];




Tổng kết

Tổng kết

hope you will help . I really need it . Thank youTải lại trang bằng JavaScript bằng location.reload rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.

sam

1,55711 silver badges14 bronze badges

Bài Viết Liên Quan

Chủ Đề