5

I have this variable and i want to add a class to it. I tryed this method, but this defenetly is not working

var someVar = '<i class="firstClass"></i>';// Html Markup to be used later.
    someVar.addClass('secondClass'); 

I need a different method to adding a new class to the html markup.

Thanks

4
  • 1
    jQuery's addClass method is only a method of a jQuery collection, and is used for adding a className to a DOM element. Commented Jan 9, 2013 at 16:38
  • You don't add classes to variable, you add classes to DOM elements. You variable needs to be a DOM element. Commented Jan 9, 2013 at 16:38
  • I've edited the content it did'yt show my full question Commented Jan 9, 2013 at 16:39
  • I know i don't have a way of actualy using addClass in this case, i'm just asking how i can do it in another way Commented Jan 9, 2013 at 16:44

4 Answers 4

3

You can only use addClass on a HTML element so you need to set your variable to be a DOM element (e.g. $('<i>')):-

var someVar = $('<i>', {'class': 'firstClass'});
someVa.addClass('secondClass');
Sign up to request clarification or add additional context in comments.

2 Comments

the <i> does not exist in the DOM it is just a html markup that will be used to append it to an object.
Thanks for the answer i just made a newVar = $(someVar); and then changed it's class.
3

When we need to get the html string from some DOM component we extracted from its parent, (eg: sortable helper), then we can convert that string to jquery object first.

example:

var thehtml = $(ui.helper).html();
    thehtml = $(thehtml); // convert to jquery object
    thehtml.addClass("yourClass"); // we can then assign new class

    ui.helper.replaceWith(thehtml); // do something you want with it

In your case

var someVar = '<i class="firstClass"></i>'; // Html Markup to be used later.
    someVar = $(someVar); // convert to jquery object
    someVar.addClass('secondClass');

Comments

2
var someVar = $('<i class="firstClass"></i>'); //embedded string in jquery object
someVar.addClass('secondClass');

1 Comment

the <i> does not exist in the DOM it is just a html markup that will be used to append it to an object.
1

You can do it in three ways:

  1. You let your HTMl markup execute first so that you can get as an DOM element and manipulate it.
  2. The hard and ugly way, treat the markup as a string and manipulate the string using 'substring' and/or 'slice'.
  3. Break up the markup and keep it in an array of strings, say array[0] = i class=; array[1] = firstClass; array[2] = /i>;

    (i m new to stackoverflow and hence dnt knw how to write HTML in the answer, but i hope u got what i m tryin to explain) Then you can add as many classes as you want by simply concatinating the classname to a array[1](array[1] += ' classname'). And when you actually want to use it, reform the whole markup by addind all the bits. It is still ugly, but you will atleast get what you want.

2 Comments

The thing is that the variable is basicali a option in a plugin i am making, that will pe set to accept only html. I made it to work by inspiring from one in the answer from the top. Thanks for taking the time to answer.
No problem. I saw the answers above and wanted to provide you with more alternatives if you wanted :)

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.