admin管理员组文章数量:1323342
I am trying to get HTML code to jquery variable and it is working in following code:
var html_var = $(this).parent().html();
But when I want to modify "html_var" I cannot do it, I mean, the html code contains button tag and I need to change this button's value and id? I tried with "find" but I don't why it didn'y work.
var modified_var = html_var.find('.button').attr('id');
I am trying to get HTML code to jquery variable and it is working in following code:
var html_var = $(this).parent().html();
But when I want to modify "html_var" I cannot do it, I mean, the html code contains button tag and I need to change this button's value and id? I tried with "find" but I don't why it didn'y work.
var modified_var = html_var.find('.button').attr('id');
Share
Improve this question
asked Oct 13, 2014 at 12:52
Umidjon UrunovUmidjon Urunov
6711 gold badge19 silver badges39 bronze badges
3
-
.html()
gets the html, not the DOM elements, so you can't find the button by class etc. – devqon Commented Oct 13, 2014 at 12:54 - Than how can I modify the content? – Umidjon Urunov Commented Oct 13, 2014 at 12:55
-
Either manipulate the DOM:
$(this).parent().find(".button").attr("id", someValue)
or use regex/replace to manipulate the html text. Depends on your use case – devqon Commented Oct 13, 2014 at 12:57
4 Answers
Reset to default 3.html() returns the html as plain text, so you can either operate on the parent object:
var $parent = $(this).parent();
$parent.find.('.button').attr('id', newValue);
or you can operate on the text
var html_var = $(this).parent().html();
//modify the text
$(this).parent().html(html_var); //replace the parent html with the modified html
var modified_var = $(html_var).find('.button').attr('id');
var html_var = $(this).parent().html(); //Get HTML code
//Modify html_var
//...
$(this).parent().html(html_var); //Reapply modification
As others have said, do not use the .html() in the html_var.
var html_var = $(this).parent();
Since it is a button tag you do not use the period/fullstop (.) Instead do this:
var modified_var = html_var.find('button');
Notice there is no (.) in the find?
To change the id do this:
modified_var.attr("id","new_id_goes_here");
To change the value of the button do this:
modified_var.html("value of button here");
To find out what the id is you can do this:
var theid = modified_var.attr("id");
EDIT:
Check out my fiddle: http://jsfiddle/r0huhx3v/8/
本文标签: javascriptJQuery assign HTML content to variableStack Overflow
版权声明:本文标题:javascript - JQuery assign HTML content to variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135436a2422347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论