views:

26

answers:

1

I want to edit some HTML I get from a var I saved. like:

var testhtml = $('.agenda-rename').html();
console.log($('input',testhtml).attr('name'));

Also tried

console.log($(testhtml).find('input').attr('name'));

But i get undefined? I figured it'd work like an $.ajax, $.get, $.post? How else can i do it?

+1  A: 

When you call .html(), you're only getting the content, not the .agenda-rename. So if the input is a direct child of .agenda-rename, then .find() won't be able to find it.

Probably better just to do without the .html() call:

var testhtml = $('.agenda-rename').clone();  // or .clone(true)
console.log($('input',testhtml).attr('name'));

Now you have the .agenda-rename element(s) and you'll be able to search for elements nested inside it/them.


EDIT: Based on comment, OP doesn't want to modify the original. As such, .clone() can be used. Answer above has been edited to reflect change.

If events are attached that should be retained, then you use .clone(true).


EDIT: The reason $('input',testhtml) and $(testhtml).find('input') give the same result is that they are actually the same thing.

jQuery converts the first version into the second behind the scenes. As such it is technically a little more efficient to use the second than the first.

Here's the code where jQuery makes the switch (after running a bunch of other tests to determine what it was passed).

http://github.com/jquery/jquery/blob/master/src/core.js#L150

patrick dw
Wont this modify $('.agenda-rename') though? I want to keep that is and not modify it. Im trying to create inline templates basically that i clone, modify, then display in this modal type interface.
Oscar Godson
@Oscar - If you don't want to modify it, you would need to `.clone()` it. If there are events attached that you want to retain, then call `.clone(true)`. I'll update.
patrick dw
@patrick dw: You are right i think, deleted :)
Sarfraz
@Oscar - If you did it the way you had it, you could use `.filter()` instead of `.find()`. The `.filter()` method filters through elements at the top level. But if there were any inputs nested deeper, it would not get those. Better to `.clone()` your element so you have what you expect.
patrick dw
Thanks man, you rock!
Oscar Godson
@Oscar - You're welcome. :o)
patrick dw
'Nother quick question though, so, var testhtml = $('.agenda-rename').clone(); testhtml = $('input',testhtml).val('test'); just returns the input with the new val(), is there a way to simply modify the html while keeping it in the var?
Oscar Godson
@Oscar - With your code, you are overwriting the element stored in `testhtml` and losing the reference to the original clone. Just treat `testhtml` like it was a typical element that you selected from the DOM. `testhtml = $('.agenda-rename').clone(); $('input',testhtml).val('test');` Now `testhtml` still references the clone of the `.agenda-rename` that you created, but the value of the `input` has been updated. If you do `console.log(testhtml)` you should see the updated HTML with the new value.
patrick dw
...If you want a separate reference to the `<input>` then use a different variable name. `var myInput = $('input',testhtml).val('test');` Now `myInput` refers to the `<input>` element you found in the clone, and `testhtml` still refers to the original clone (with that updated `<input>`).
patrick dw