I created a simple program to showcase the issue I'm currently facing.
- Essentially, in this program, I input a name and a color which are then added to an unordered list when the submit button is clicked.
- Afterwards, I attempted to change the color of the sentence using a jQuery id selector, but the color doesn't update. However, using
getElementById
works correctly. - For example, if I input "Tom" as the name and "red" as the color, a red sentence should be displayed.
Here is the version using a jQuery selector:
$(document).ready(function () {
// initialization
$('select').formSelect();
$('#myForm').submit(function (e) {
let name = $('#name').val();
let color = $('#color').val();
addToList(name,color);
e.preventDefault();
});
});
function addToList (n,c) {
$('#main').append(`<li id=${n}**${c}>My name is ${n}. My favorite color is: ${c}</li>`);
$(`#${n}**${c}`).css({'color':`${c}`});
// document.getElementById(`${n}**${c}`).style.color = c;
}
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Lab</title>
</head>
<body>
<div class="container">
<form id="myForm">
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" required>
</div>
<div class="input-field">
<label for="color">Color</label>
<input type="text" id="color" required>
<button class="btn" type="submit">Submit</button>
</div>
<ul id="main"></ul>
</form>
</div>
</body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="app.js"></script>
</html>
Below is how I utilized getElementById
, where it works successfully:
$(document).ready(function () {
// initialization
$('select').formSelect();
$('#myForm').submit(function (e) {
let name = $('#name').val();
let color = $('#color').val();
addToList(name,color);
e.preventDefault();
});
});
function addToList (n,c) {
$('#main').append(`<li id=${n}**${c}>My name is ${n}. My favorite color is: ${c}</li>`);
// $(`#${n}**${c}`).css({'color':`${c}`});
document.getElementById(`${n}**${c}`).style.color = c;
}
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Lab</title>
</head>
<body>
<div class="container">
<form id="myForm">
<div class="input-field">
<label for="name">Name</label>
<input type="text" id="name" required>
</div>
<div class="input-field">
<label for="color">Color</label>
<input type="text" id="color" required>
<button class="btn" type="submit">Submit</button>
</div>
<ul id="main"></ul>
</form>
</div>
</body>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="app.js"></script>
</html>