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 using jQuery – 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
Add a ment  | 

1 Answer 1

Reset to default 5

Lets 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