Replace text in HTML Elem using JQuery

Last post 07-05-2009 11:46 AM by babupalladium1. 5 replies.

Sort Posts:

  • Replace text in HTML Elem using JQuery

    07-03-2009, 4:35 PM

    I have a jQuery jsTree on my page.  The tree works fine as expected.  I am having some trouble modifying the node's title text programatically.  jsTree does not have a built-in functionality (not in 0.9.8) to do this.  So, I am trying to manually find the node's innerHTML and change the title and save it back.  My innerHTML looks something like this:

    <A href="#" jQuery1246640720277="25">electronics</A>

    I want to change the text "electronics" to something else programatically based on some other user action.

    When I use innerText or text() methods, I can replace the text but I loose all the other attributes of the innerHTML.  How can I preserve all the attributes of this html tag but be able to just modify the displayed text?

    Any help please.....


    Babu.

  • Re: Replace text in HTML Elem using JQuery

    07-03-2009, 9:38 PM
    • Contributor
      4,040 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 607

    Either replace the whole anchor tag like with new text. For example if you want to change "electronics" with "crockery" write something like 

    div1.innerHTML='<A href="#" jQuery1246640720277="25">crockery</A>';


     

    Or give the anchor tag an id and access it and set its innerHTML or innerText properties like

    <A id="a1" href="#" jQuery1246640720277="25">electronics</A>
    
    a1.innerHTML="crockery";

    Hope it helps

    MARK AS ANSWER if it helps
  • Re: Replace text in HTML Elem using JQuery

    07-04-2009, 7:46 AM
    • Member
      11 point Member
    • babuman1
    • Member since 07-09-2006, 5:17 PM
    • Posts 9

     Thanks for the reply deepthoughts.  But my problem is the whole tag is created automatically by jsTree component which does not create an id for the anchor tag. 

    The first line gives me the whole anchor tag.  The second line gives me the displayed text which is what I want to change.  On the third line, I modify the text.  On the fourth line, I replace the innerText with the modified text.  But when I check the variable a4 it still is equal to a1.  I tried to use the jQuery's text() method with the .innerText.  But then, the whole anchor tag is replaced with just the text.  No tags.

    var a1 = node[0].innerHTML;
    var a2 = node[0].innerText;
    var a3 = a2 + "(test)";
    node[0].innerHTML.replace(a2.innerText, a3);
    var a4 = node[0].innerHTML;


     

    Babu
  • Re: Replace text in HTML Elem using JQuery

    07-04-2009, 8:14 AM
    Answer
    • Contributor
      4,040 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 607

    You'll have to assign the changed value back to the node so that it is actually replaced. Like

    node[0].innerHTML=node[0].innerHTML.replace(a2.innerText, a3);

    Please check and let me know.

    Hope it helps.

    MARK AS ANSWER if it helps
  • Re: Replace text in HTML Elem using JQuery

    07-05-2009, 11:39 AM
    • Member
      11 point Member
    • babuman1
    • Member since 07-09-2006, 5:17 PM
    • Posts 9

     deepthoughts,

    It worked.  Thank you.

    Babu
  • Re: Replace text in HTML Elem using JQuery

    07-05-2009, 11:46 AM

    By the way, the "Mark as answer" option is not visible in IE8.  I had to open this thread in FireFox in order to do that.

    Babu.

Page 1 of 1 (6 items)