1

i am storing element id in a variable globe and then applying css but its not working. if i apply css to 'img' then its works ,but when i am trying to apply with variable its not working . i have check console globe value contains id just css is not getting applied .

var selglobe=$('#' + globe);
                      $('#one').remove();
                      $(selglobe).addClass('anam');
                      $(selglobe).css("border","double");
                      $(selglobe).css("border-color","yellow");
                      console.log('HTML Globe value is '+globe);

EDIT:

var selglobe= $('#' + globe);
$('#one').remove();
selglobe.addClass('anam');
selglobe.css("border","double");
selglobe.css("border-color","yellow");
console.log('HTML Globe value is ' + globe);
1
  • Everyone i have correct my code ... but its not applying css to particular object by using id?? Commented May 23, 2013 at 6:52

4 Answers 4

2
var selglobe = $('#' + globe);

Here selglobe is already an jQuery object. You don't need to do $(selglobe) again to make it a jQuery object.

You can simply do this:

var $selglobe = $('#' + globe);
$('#one').remove();
$selglobe.addClass('anam');
$selglobe.css("border", "double");
$selglobe.css("border-color", "yellow");
console.log('HTML Globe value is ' + globe);

or better:

var $selglobe = $('#' + globe);
$('#one').remove();
$selglobe.addClass('anam').css({border: 'double', borderColor: 'yellow'});
Sign up to request clarification or add additional context in comments.

Comments

2

Please try the code below:

var selglobe= $('#' + globe);
$('#one').remove();
selglobe.addClass('anam');
selglobe.css("border","double");
selglobe.css("border-color","yellow");
console.log('HTML Globe value is ' + globe);

The variable selglobe is turned into a jQuery object at the first line so you do not need to use the $(selglobe) afterwards anymore since it is already a jQuery object.

2 Comments

@KennyDs i have udated my code ... still its not applying css to that particular object .
Please provide a JSFiddle example ;)
1

Change var selglobe=$('#' + globe); to var selglobe= '#' + globe;

1 Comment

That would get the element each time. Waste of processing
1

try this

                  var selglobe='#' + globe;
                      $('#one').remove();
                      $(selglobe).addClass('anam');
                      $(selglobe).css("border","double");
                      $(selglobe).css("border-color","yellow");
                      console.log('HTML Globe value is '+globe);

replace var selglobe=$('#' + globe); with var selglobe='#' + globe;

or

 var selglobe= $('#' + globe);
$('#one').remove();
selglobe.addClass('anam');
selglobe.css("border","double");
selglobe.css("border-color","yellow");
console.log('HTML Globe value is ' + globe);

1 Comment

That would get the object each time. That is wasteful

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.