2

I can't for the life of me figure out what I'm doing wrong here? Why wouldn't this work? I'm trying to manipulate an HTML string with jQuery, I ultimately am going to insert history data within the id=hist, but right now I'm just trying to get it to manipulate the HTML string and can't.

var hist = '<div class="panel panel-default"> \
            <div class="panel-heading" style="background-color:#FFFF00">History</div> \
            <div class="panel-body" id="hist"> \
            </div> \
        </div>';

$(hist).find("div").addClass("test");
console.log(hist);

The output is the exact same as the hist variable. NO changes? I know there's a simple answer I just can't figure it out. Obviously jQuery does not return a variable it should directly affect the hist input, right?

4
  • 4
    "Obviously jQuery does not return a variable it should directly affect the 'hist' input, right" Obviously that is incorrect, as otherwise hist wouldn't contain a string anymore. It's crazy to think that any method would be able to manipulate a variable containing a string directly, strings are base values, the only time it would retain it's value and just update would be if you're updating a property on an object. Commented Sep 17, 2015 at 14:47
  • I meant return variable as in var new_hist = $(hist).find("div").addClass("test"); So what are you saying? It's not possible? Commented Sep 17, 2015 at 14:51
  • 1
    No, if you have a return variable then it'l work. Commented Sep 17, 2015 at 14:52
  • 1
    console.log($(hist).find("div").addClass("test")); Commented Sep 17, 2015 at 14:52

3 Answers 3

4

Well, it's due to the fact that you're trying to print the original String. However, Strings are immutable in JavaScript.

Try to do this instead:

var elem = $(hist);
elem.find("div").addClass("test");
console.log(elem);
Sign up to request clarification or add additional context in comments.

5 Comments

that did it! Thank YOU!
which is the same as what I posted 6 mins before you, I just used $.parseHtml instead.
@NickDewitt Yes, all of the answers are correct, the op can choose whichever he/she wants, whichever he/she found was most helpful.
@NickDewitt I tried the parseHtml and it didn't seem to work? I apologize if they are both correct, I am still learning jQuery and can be confusing. If they are both correct it was certainly my fault attempting to use it.
@Chad no worries mate, I chucked it in a fiddle to show it works now anyways.
3

The variable isn't a string, it contains a string. Your method took that string, converted it into a DOM fragment, and then returned that dom fragment wrapped in a jQuery object. Therefore, you'll have to store the dom fragment in the variable in place of the original string, or store it in a new variable.

var somevar = $(hist).find("div").addClass("test").end();

console.log($("<div>").append(somevar).html());

Strings cannot be manipulated, instead, when you do anything with a string, the result is a new string or value, not the original. Therefore, there's no way to modify the value of the string in the variable without replacing what's stored in that variable. The same is true for integers, floats, undefined, and null. (i might have missed one or two.) Objects on the other hand CAN be manipulated, so anything that's based on objects (arrays, functions, etc) can be manipulated without creating a new object.

4 Comments

not sure why but when I tried this method it manipulated the entire dom? the log showed the entire container? I really appreciate your help an insight.
I suspect you forgot the < and > surrounding the div on the last line. Basically, i created one more dom fragment that would hold your dom fragment so that i could easily get the entire dom fragment back into htmlstring form. You could also just console.log(somevar) to get it as a dom frag wrapped in a jQuery object instead.
just tried it again, strange it added the test class to the inner divs not all? I'm assuming that's what .end() did? Not really sure.
No, your snippet is supposed to only add it to inner divs. .find() will only find inner divs of the outer, not including the outer.
2

Have you tried parsing the string first?

var html = $('<div class="panel panel-default">')
$(html).find("div").addClass("test");
alert(html.prop('outerHTML'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

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.