Assigning a class to an element based on the condition of another element within a directive's template

My goal is to create a directive that incorporates a label and a text input field. The main requirement is for the label to be visible only when the input field is in focus or contains text. Below is the current code I am working with, which I aim to convert into a reusable directive for other fields.

<label class="text-field-top-label"></label>
<input  type="email"
        name="email"
        ng-model="user.email"
        ng-model-options="{updateOn: 'blur'}"
        required
        placeholder="Email address"/>

I am facing difficulty in this task because I cannot directly target the label or text field without assigning them specific IDs, which goes against my intention of creating a flexible directive. If you have any insights or suggestions on how to tackle this issue, I would greatly appreciate it.

Answer №1

Here is my unique solution for this issue: I utilized a mix of CSS and Vue.js to tackle the problem. First, I hid the label by setting its display property to none and then applied the following CSS styles:

&:focus::-webkit-input-placeholder { color:transparent; }
&:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
&:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
&:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */
&:focus + label {
  display: inline-block;
}

After that, in the HTML section, I included the following code snippet:

<input type="text"
       id="username"
       v-model="user.username"
       placeholder="Enter your username"/>
<label class="input-label"></label>

This piece of code hides the placeholder when the input is focused and displays the label above the input field. To fulfill the second requirement of displaying the label if the field has a value, I checked the model using Vue.js. The condition checks whether the field exists, has a value other than an empty string, and doesn't have any validation errors apart from "required". This way, the label only shows up when all conditions are met.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clickable image in HTML that sends a request to an ActionResult

In my MVC project, I am attempting to create an HTML image button within a search box that will redirect to an ActionResult. My current setup looks like this: <input type="text" id="Text1" name="seachKeyword" class="searchTextBox" value="Search" style ...

Adjusting the height of the v-footer

I'm having trouble adjusting the height of the v-footer component. According to the documentation, I should be able to use the height prop, but when I try height="100px" or height="50%", the footer doesn't change. What could be causing this issue ...

Creating PDFs with html2fpdf using the power of PHP, jQuery,

I am trying to utilize the jQuery code below to send a request to an external php file named html2fpdfconverter.php: $("#exportentry").click(function(e){ e.preventDefault(); if(submitted){ //export var data = $("#container").html( ...

Adjusting the color of a Bootstrap card's header

I've been struggling with customizing the colors of a bootstrap card header in my application. Here's what I have so far: https://i.sstatic.net/Lv1jk.png Currently, the code I'm using looks like this: <div class="card"> ...

Showcasing images in Flask

Hello everyone, I am encountering an issue with displaying images in my HTML file. Currently, I am using Flask within Visual Studio Code and here is the code snippet that I want to display: <img src="/templates/WANG_CHONG_st_1.png" width=" ...

Flashing text compatibility across all browsers

I'm looking for a way to create text that blinks on all major web browsers. Initially, I attempted using the <blink> HTML tag, but unfortunately, it only works in Mozilla Firefox. Next, I experimented with CSS: <style> .blink {text-deco ...

Ways to utilize jquery script within a react component

<script> $(document).ready(function() { var source; var destination; var fn = function(event, ui) { toDrop = $(ui.draggable).clone(); if ($("#droppable").find("li[uniqueIdentity=" ...

I'm attempting to implement a mode rewrite rule in Wordpress, but it doesn't seem to be working as expected

I am attempting to forward all requests from /app/.... to a particular page - this will be used later for angular js. Here is what I have tried: add_rewrite_rule('/app/.*', 'index.php?page_id=11', 'top'); Unfortunately, thi ...

The use of event.returnValue is outdated and no longer supported. It is recommended to use the standard event.preventDefault() method instead. You may encounter

Hey there :) Currently, I am utilizing JQuery 1.9.1.js to search records using JSON. I am able to retrieve the search list locally, but when attempting to publish it on Windows Server 2008 and IIS 7, I face issues as it throws an error stating "event.ret ...

Fixed top bar and navigation menu, with a body that can be scrolled

My current goal is to achieve the following layout: <div style="position:absolute; background-color:Transparent; left:0px; right:0px; height:100px; z-index:2;"> <div style="background-color:Silver; width:1000px; height:100px; margin:0 auto;"& ...

Setting the height to "auto" will take precedence over any other

I recently designed a responsive webpage with two distinct sections. In order to ensure all elements in the right section were responsive, I utilized the following CSS code: #rightSection * {max-width:100%; height:auto;} Despite this, I noticed that any ...

Unable to select the uib-tabs element link with Protractor

<ul class="nav nav-pills" ng-class="{'nav-stacked': vertical, 'nav-justified': justified}" ng-transclude=""> <li ng-class="[{active: active, disabled: disabled}, classes]" class="uib-tab nav-item ng-scope ng-isolate-s ...

The alignment of Bootstrap 4 cards is not coming together as expected

Good morning, I'm currently working on organizing my Bootstrap card elements into rows of 3 cards each. I want the cards to be uniform in height and width, with spacing between them. For instance, if I have 7 cards, I'd have 3 rows of three card ...

Best practices for aligning elements within a flex container

Trying to align a button at the bottom using flexbox, but running into issues. Tried using position:absolute and bottom: 0, but it seems that the browser is not recognizing it correctly. <ul class="box"> <li><div>this has <br&g ...

The $http patch request is failing to transmit data to the server

Recently, I started utilizing AngularJS to create a CRUD functionality using Laravel and AngularJS. I have implemented a function that serves the purpose of both updating and saving data. Here is my function code: $scope.save = function (modalstate, id) ...

Protractor tests encounter issues when working with Select2 within a modal in Angular

I am facing a challenge while running my angular e2e tests using protractor. In some cases, I have a select element inside a modal. The issue arises when the test fails to locate the select element with the following error message: NoSuchElementError: No ...

designing a parallax footer that stays hidden under the surface?

I'm completely new to asking questions here, but I've gained valuable knowledge from browsing the site. Thank you! Just stumbled upon this amazing 'under the rug' style parallax effect on a footer and I'm wondering how it was crea ...

"Learn the art of combining an ajax request with a captivating background animation at the same time

I'm currently working with the following code snippet: $('#contentSites').fadeOut(200, function() { // Animation complete $.get("my_account_content.php?q=" + content, function(data){ $("div#contentSit ...

Guide to adding a new font to your Ember project

I am currently in the process of developing a website utilizing the MEEN Stack framework (Mongo, Ember, Express, Node). One challenge I am encountering is importing the Lato font family into my project to serve as the main font for the entire site. Despite ...

The fixed navbar in Bootstrap is missing a bottom margin

I have created a basic theme using bootstrap 4. The navbar in this theme has a fixed-top class. Right below the navbar, there is a div that displays the page title as its content. <div class="aloldal_text_div"> <div class="container"> < ...