0

I am new to Javascript, I am using Backbone So I add a style like this(to a link when its clicked)

     $(eventC.target.parentNode).css('border', '1px solid red');

I want the previous link to be cleared of the red border I added when I click on new link

I looked at link below jQuery - remove style added with .css() function

So in essence I want to do this:

$(OLDLINK).css('border', 'none');
$(NewLINK).css('border', '1px solid red');

with events I get from backbone

Is there a simple way to do this? or is this approach just wrong?

Community
  • 1
  • 1
Jack
  • 526
  • 3
  • 10
  • 30

4 Answers4

4

I suggest using a CSS class for adding the border and to add / remove it like below

CSS:

.borderRed
{
  border : 1px solid red;
}

jQuery:

//remove border from old link
$('.borderRed').removeClass('borderRed');

// add border in new link
$(eventC.target.parentNode).addClass('borderRed');
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
2

If I clearly understand your question, you need something like this

var container = $("div.parent").on("click", "a", function(){
    $(this).addClass("selected");
    container.find("a").not($(this)).removeClass("selected");
});

demo

Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
1

This solution may be as:

Backbone.View.extend ({

 events: {
   "event-which-delete-border a.link":"changeLink",
   "event-which-add-border a.link":"changeLink"
 },
changeLink:function(event) {

 if (event.type == 'event-which-delete-border') {
   $('a.link').css('border', 'none');
 } else if (event.type == 'event-which-add-border')
   $('a.link').css('border', '1px solid red');
 }

})

In this example, words 'event-which-delete-border' and 'event-which-add-border' may be all events from JavaScript, such as 'click' 'mouseover' and etc

Waldo Jeffers
  • 2,289
  • 1
  • 15
  • 19
BebnevRoman
  • 315
  • 2
  • 6
1

You can use any of these three methods-

  1. you can use the .attr() to change the style of the element.

    $(OLDLINK).attr('style', 'border: none');
    $(NewLINK).attr('style','border: 1px solid red');
    

    this method is safe to cross browser defects.

  2. the one you are using.
  3. you can use the method as explained by Bhushan above.

the difference between these could be found here- Visibility attribute question

Community
  • 1
  • 1
anubhs
  • 566
  • 1
  • 8
  • 26