1

I am trying to get id of clicked button and with that case I want to change change/add class of specifiv div.

Here is the code:

$("input").click(function (e) {
    var idClicked = e.target.id;
    switch (idClicked) {
        case 'regiesterButton':
            $('regiesterAlert').css('class', 'alert alert-danger');
            $('regiesterAlert').append('<b>Error</b> Blabla')
            break;
        case 'loginButton':
            break;
        case 'loginButton':
            break;
    }
});

EDIT: After it steps into case there is exception:

if(!jQuery)throw new Error("Bootstrap requires jQuery");+function(a).....

What`s wrong in the code?

3
  • regiesterAlert is class or id? use .regiesterAlert if class and #regiesterAlert if id Commented Oct 31, 2015 at 12:49
  • When the problem is with jQuery interacting with DOM, you should also include the corresponding HTML in the question. Commented Oct 31, 2015 at 12:57
  • 1
    Don't add new question as EDIT: I'm having this problem too, ask another question. Commented Oct 31, 2015 at 12:58

3 Answers 3

2

For classes you should use . and for id #

$('.regiesterAlert').css('class', 'alert alert-danger');
   ^
   |
$('.regiesterAlert').append('<b>Error</b> Blabla')
   ^
   |

also instead of .css('class') you should use .addClass, and instead of using same selector twice you can use it once

$('.regiesterAlert')
  .addClass('alert alert-danger')
  .append('<b>Error</b> Blabla')
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$('.regiesterAlert').addClass('alert alert-danger');
$('.regiesterAlert').append('<b>Error</b> Blabla')

1 Comment

Add the explanation with code. Only code-dumps with Try this are not helpful.
0

use

$('.regiesterAlert').addClass('alert alert-danger');

1 Comment

Welcome to SO! Although this will solve the problem, please add some explanation of what OP is doing wrong and how to solve that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.