How to add a pseudo-element in jquery

Discussion in 'JavaScript' started by makamo66, Jun 3, 2016.

  1. #1
    $('.email').blur(function() {
    if (!validateEmail($(this).val()) ) {
    $(this).removeClass("blur").addClass("focus");
    } else {
    $(this).removeClass("focus").addClass("blur");
    }
    })
    
    <input type="email" size="61" class="email"></input>
    
    .focus{border: 1px solid red;outline: none;}
    .blur{border: 1px solid #7dc855;outline: none; }
    
    .focus:after{
    content: "\00D7";
    position: relative;
    top: -24px;
    left: 500px;
    color: red;
    }
    
    .blur + label:before{
    content: "\2713";
    position: relative;
    top: -24px;
    left: 500px;
    color: #7dc855;
    }
    Code (markup):
    When my email doesn't validate I want to add the red border from the class focus and an X mark from the pseudo-element before. The code shows the red border but it doesn't show the X mark. How do I add the pseudo-element to my text box?
     
    makamo66, Jun 3, 2016 IP
  2. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    \00D7 is the X mark.
     
    makamo66, Jun 4, 2016 IP
  3. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #3
    input can't contain other elements. You have to add some elements to hold the error messages. Hope this helps.
     
    Blizzardofozz, Jun 13, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Basically, set position: relative; on the input in question, and use a <span> with position: absolute; and position calculated by javascript for finding where exactly you want to place the X - or just put the element outside the input (before or after).
     
    PoPSiCLe, Jun 13, 2016 IP
  5. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Thank you for your help.
     
    makamo66, Jun 13, 2016 IP