Thuộc tính size của đối tượng Input URL trong HTML DOM trả về hoặc thiết lập chiều rộng hiển thị (số ký tự nhìn thấy) của ô nhập URL. Giá trị mặc định là 20 nếu không khai báo.
Cú pháp
Lấy giá trị size
inputURLObject.size
Thiết lập size
inputURLObject.size = số_nguyên
Ví dụ minh họa
Ô URL ban đầu size=10. Nhấn nút tăng visibility lên 30:
<!DOCTYPE html>
<html>
<head>
<title>Input URL size</title>
<style>
form { width: 70%; margin: 0 auto; text-align: center; }
* { padding: 2px; margin: 5px; }
input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
<form>
<fieldset>
<legend>URL-size</legend>
<label for="URLSelect">URL: <input type="url" id="URLSelect" size="10"> </label><br>
<input type="button" onclick="tangKichThuoc()" value="Tăng độ rộng">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputURL = document.getElementById("URLSelect");
function tangKichThuoc() {
inputURL.size = 30;
divDisplay.textContent = 'Độ rộng tăng lên: ' + inputURL.size;
}
</script>
</body>
</html>
Kết quả
- Ban đầu: ô URL hiển thị 10 ký tự.
- Nhấn Tăng độ rộng: ô mở rộng thành 30 ký tự, hiển thị "Độ rộng tăng lên: 30".
Lưu ý: size chỉ ảnh hưởng giao diện, không giới hạn độ dài nhập. Dùng maxlength để giới hạn ký tự.