admin管理员组文章数量:1400033
I want to change the color of the message
variable in the following HTML:
<span id="mess" style="visibility:visible;">
<text id="tex">{{ message }}</text>
</span
I am using Jinja2 together with Flask - Python to pass a value to the {{ message }}
variable. Here is how I tried to do it:
$(document).ready(function(){
if (document.getElementById('tex').value == 'Message sent !')
{
document.getElementById('tex').setAttribute("style", "color:green;");
}
else
{
document.getElementById('tex').setAttribute("style", "color:red;");
}
});
The result of the document.getElementById('tex').value
is always undefined
and the color of the text of the message
variable is always red.
Is there a way with which I can acplish that ? Thank you in advance.
I want to change the color of the message
variable in the following HTML:
<span id="mess" style="visibility:visible;">
<text id="tex">{{ message }}</text>
</span
I am using Jinja2 together with Flask - Python to pass a value to the {{ message }}
variable. Here is how I tried to do it:
$(document).ready(function(){
if (document.getElementById('tex').value == 'Message sent !')
{
document.getElementById('tex').setAttribute("style", "color:green;");
}
else
{
document.getElementById('tex').setAttribute("style", "color:red;");
}
});
The result of the document.getElementById('tex').value
is always undefined
and the color of the text of the message
variable is always red.
Is there a way with which I can acplish that ? Thank you in advance.
Share Improve this question asked Dec 5, 2017 at 22:50 user6420403user6420403 3-
Why do you want to use
Vanilla JS
if you are also usingjQuery
– Sushanth -- Commented Dec 5, 2017 at 22:51 - It is a project I am doing for practice purposes and I wanted to practice on some Javascript as well. That's the only reason why. – user6420403 Commented Dec 5, 2017 at 22:53
-
<text>
isn't a standard tag, and only form fields have values. It would be much cleaner to set a class with Flask and style it with CSS instead of changing the color with JavaScript. – JJJ Commented Dec 5, 2017 at 22:53
1 Answer
Reset to default 5Lets use contains selector.
$(document).ready(function() {
if ($('#tex:contains("Message sent !")').length) {
$('#tex').css('color', 'green');
} else {
$('#tex').css('color', 'red');
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="mess" style="visibility:visible;">
<text id="tex">Message sent !</span>
</span>
本文标签: javascriptChange color of span tag depending on valueStack Overflow
版权声明:本文标题:javascript - Change color of span tag depending on value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744119536a2591671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论