0
<li data-id="528">
  <a title="LOGIN" class="dropdown-toggle" href="#">
          <i class="glyphicon glyphicon-user"></i>LOGIN</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

I Have this html I want to change the content by data-id like given below

<li data-id="528">
  <a title="LOGOUT" href="/logout">
          <i class="glyphicon glyphicon-user"></i>LOGOUT</a>
  <div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>
</li>

Is this possible if, Please advice me

Thank you.

Umashankar Saw
  • 1,131
  • 1
  • 9
  • 25

4 Answers4

4

Yes it's possible.

Like this

var a=$('li[data-id="528"]').find('a');
a.attr('href','/logout');
a.contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace('LOGIN','LOGOUT');
});
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
4

You can get the element by attribute selector, and use .html() to modify content:

$('[data-id="528"]').html('<a title="LOGOUT" href="/logout"><i class="glyphicon glyphicon-user"></i>LOGOUT</a><div style="width: 350px;" data-width="350" data-class="menu-login-row">My other content</div>')

Actually you can modify innerHtml of an jquery element by .html()

$(selector).html(whatEverYouWant);
Sing
  • 3,942
  • 3
  • 29
  • 40
  • @debin please note that the OP question is How to change the "innerHtml content", not mention about what you are saying, plus, you only need to wrap what you want in .html() is the same. – Sing Jan 12 '16 at 05:51
  • He wants to change innerHTML like `
  • LOGOUT`
  • – Dhara Jan 12 '16 at 05:53
  • I think he want to know how to change the content by data-id, so what I mean is op can use .html() to change to anything he wants. But I modify answer as your mention :) – Sing Jan 12 '16 at 05:57
  • He specified what he wants in innerHTML in `data-id` :) – Dhara Jan 12 '16 at 05:58