Alter the class on a mouse click event in the text input

<input type="text" onkeypress="return buyukHarf(event);" class="green" name="f1"/>

Is there a way to modify the green class or alter the border color of the input field when it is clicked? Any suggestions would be greatly appreciated! Thanks in advance.

Answer №1

$("button").on("click", function(){
    $(this).removeClass("blue").addClass("red");    
});

When dealing with interactive elements, I prefer using mouseenter() and mouseleave() instead of a click() event.

It's also recommended to steer clear of inline javascript like the one in your code snippet.

Example using Mouseenter/Mouseleave:

$("button").mouseenter(function(){
    $(this).removeClass("blue").addClass("red");
}).mouseleave(function(){
    $(this).removeClass("red").addClass("blue");
});

Answer №2

Give this a shot:

$( "button" ).mousedown( function() {
    $( this ).addClass( 'newClass' );
} );

Answer №3

Give this a shot:

$("input[name='f1']").click(function(){
    $(this).removeClass("green").addClass("red");    
});

Alternatively // to change the border

$("input[name='f1']").click(function(){
    $(this).removeClass("green").css("border", "1px solid red");    
});

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

What is the process of embedding a Vue.js application into an already existing webpage using script tags?

My goal is to integrate a Vue.js application into an existing webpage using script tags. Upon running `npm run build` in my Vue application, I end up with 7 compiled files: app.js app.js.map manifest.js manifest.js.map vendor.js vendor.js.map app.css In ...

Convert a list into nested JSON items by utilizing the getJSON function

Below is an example of a JSON object: {"sessions":[ { "totalusers":"2", "Users": [ { "id": 1, "name": "abc" }, { "id": 2, "name": "def" } ] } ]} Here is the corresponding HTML structure: <div> ...

Is it better to use remove() or destroy() in TinyMCE 4?

I am currently using the TinyMCE editor from TinyMCE, and I am looking to remove or destroy the editors on my page, which contains multiple editors. I also want to get rid of any classes and IDs that were added by TinyMCE. However, I want to keep the edit ...

Tips for placing a button on top of a Div element with overflow set to hidden?

My goal is to have a button placed over a box, but I am facing difficulty achieving this. I have experimented with z-index, grid-content, and other methods, but the button refuses to show over the box as desired. The placement of the button inside the box ...

Can you explain the significance of `arr[i]` in JavaScript?

const sentence = 'i have learned something new today'; const words = sentence.split(" "); for (var j = 0; j < words.length; j++) { words[j] = words[j].charAt(0).toUpperCase() + words[j].slice(1); } const newSentence = words.jo ...

Increasing the depth of JSON objects through multiple levels

Looking to expand upon an existing empty JSON object. After a couple of basic extensions, I end up with {size: 10, from: 0}. However, when attempting to use the $.extend function with multi-level JSON "query" : { "query_string" : ...

Tips for sending a JavaScript variable value to jQuery validate() submitHandler while performing an AJAX request

My validation on ajax calls is currently using a jQuery plugin. This is the current setup: $("#invoiceForm").validate({ rules: { plateNumber: { required: true, }, plateIssueState: { required: true, } }, ...

Nodejs: Implementing Context similar to React

I'm trying to achieve a similar functionality to React Context in Node.js but facing difficulties. In React, by creating a single context, I can provide and consume different values in my components based on the value passed to the <Provider/>. ...

Is it considered acceptable to include a JSONP request within another JSONP request?

Can you confirm if the code below is valid? $.getJSON(server_url+"?callback=?",{id:deleteId,pw:password,action:"editpassword"},function(data){ $.getJSON(server_url+"?callback=?", {id:deleteId,pw:password,action:"editpassword"},function( ...

The color of the last clicked DIV is supposed to stay permanent, but for some unknown reason

I'm attempting to replicate this design in my project: http://jsfiddle.net/AYRh6/26/ However, I am facing issues with it and cannot pinpoint the exact problem in the code. I am using code for a toggle effect. Any assistance would be greatly appreciat ...

I'm struggling to connect two pages in HTML

Hi there, I'm in the process of building my very first website and I've hit a snag. I have all the HTML pages ready to be linked together, but for some reason when I click on "contact", it just won't open that page. I've searched high a ...

What is the process behind Twitter's ability to quickly show my profile?

Scenario I was intrigued by the different loading times of Twitter's profile page based on how it is accessed: Clicking the profile link in the menu results in a 4-second load time with DOM and latest tweets loade ...

Displaying checkboxes according to one array, and marking selected items based on another array

Desired outcome: Show 4 checkboxes (based on this.state.people): Paul Martin Joseph Gregor According to this.state.peopleChecked, the following are checked: Martin Gregor Access the code here: https://stackblitz.com/edit/react-bootstrap-examples ...

The AJAX request made right before submitting the form is not functioning

I have a Spring MVC - JSP web application in which I need to populate a text value input with JS/jQuery before submitting a form. The information for this input comes from an AJAX call that should be triggered when the submit button is clicked, but before ...

Angular2 (RC5) global variables across the application

I am seeking a solution to create a global variable that can be accessed across different Angular2 components and modules. I initially considered utilizing dependency injection in my `app.module` by setting a class with a property, but with the recent angu ...

What is the best way to transfer alt text from one element to my div?

I am attempting to modify the alt text and background image of a specific div with the ID of "image" using JavaScript. My first attempt was to use document.getElementById('image').innerHTML = this.alt, but unfortunately, it did not produce the d ...

Implementing a Model-View-Controller architecture with AJAX functionality

Recently, I encountered an issue while trying to refresh a div containing a PartialView using Ajax.BeginForm. This process had worked smoothly for me numerous times in MVC4, but with the switch to MVC5, things took a turn. Let me walk you through the step ...

Creating a functional component in React using TypeScript with an explicit function return type

const App: FC = () => { const addItem = () => { useState([...items, {id:1,name:'something']) } return <div>hello</div> } The linter is showing an error in my App.tsx file. warning There is a missing return type ...

How does jquery's removeClass function on a button continue to function smoothly even after an ajax call

Code Snippet: <span class="input-group-text"> <button type="button" class="btn btn-outline-danger btn-sm owner_button owner_check" name="owner_check" > <i class="far fa-check-circle"></i> </button> </span> ...

Error: Unable to locate module: 'material-ui/styles/colors'

I encountered an issue with the code below, as it failed to compile: import React from 'react'; import { AppBar, Toolbar } from 'material-ui'; import { Typography } from 'material-ui'; import { MuiThemeProvider, createMuiThem ...