• Text 1
  • Text 2
  • Text 3
  • Text 4
    • Text 1
    • Text 2
    • Text 3
    • Text 4
      • Text 1
      • Text 2
      • Text 3
      • Text 4
        DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        Iterate over <li> and replace text with jquery

        286 Views Asked by middleendian At 19 August 2016 at 11:39 2025-12-10T13:32:36.571000

        I have the following HTML structure:

        <div class="mydiv">
          <ul>
            <li>Text 1</li>
            <li>Text 2</li>
          </ul>
          <ul>
            <li>Text 3</li>
            <li>Text 4</li>
          </ul>
          <ul>
            <li>Text 5</li>
          </ul>
        </div>
        

        I want to find a string containing "4" and replace it with "four". Other strings from other li elements of this div are then no longer interesting for me. How can I do this if I only know in which div I should search but not which li element?

        jquery html replace iterated-function
        Original Q&A
        2

        There are 2 best solutions below

        1
        Kld Kld On 19 August 2016 at 11:43 BEST ANSWER

        You can use the :contains selector

        $( "div li:contains('4')" ).text(function(_, val) { 
            return val.replace(/4/, "four")
        });
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="mydiv">
          <ul>
            <li>Text 1</li>
            <li>Text 2</li>
          </ul>
          <ul>
            <li>Text 3</li>
            <li>Text 4</li>
          </ul>
          <ul>
            <li>Text 5</li>
          </ul>
        </div>

        1
        gavgrif gavgrif On 19 August 2016 at 11:59

        Just use replace on the HTML of the parent div and then reset the HTML using the replaced content:

        $(".mydiv" ).html($(".mydiv" ).html().replace(/4/, "four"))
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="mydiv">
          <ul>
            <li>Text 1</li>
            <li>Text 2</li>
          </ul>
          <ul>
            <li>Text 3</li>
            <li>Text 4</li>
          </ul>
          <ul>
            <li>Text 5</li>
          </ul>
        </div>

        Related Questions in JQUERY

        • How to sort these using Javascript or Jquery Most effectively
        • Ajax jQuery firing multiple time display event for the same result
        • .hover() seems to overwrite .click()
        • Check for numeric value with optional commas javascript
        • Extending Highmaps Side Effect
        • Array appending after each onclick and loop in javascript
        • how can i append part of a table based on how many tr it has?
        • Play multiple audio files in a slider
        • Remove added set of rows
        • Access property of an object of type [Model] in JQuery
        • AJAX PHP - Reload div after submit
        • proengsoft/laravel-jsvalidation ReferenceError: jQuery is not defined
        • when a checkbox is checked how to display a different hidden element using javascript
        • Get jquery error Uncaught RangeError: Maximum call stack size exceeded
        • Removing only the closest thead on table filtering

        Related Questions in HTML

        • Delay in loading Html Page(WebView) from assets folder in real android device
        • Why does a function show up as not defined
        • CSS Class is not applying to element (border width,color,and style attributes)
        • How to sort these using Javascript or Jquery Most effectively
        • how to fill out the table with next values in array with one button
        • Automatically closing tags in form input?
        • Positioning child at bottom of parent with scroll
        • Remove added set of rows
        • Website zoomed out on Android default browser
        • Twitter Bootstrap horizontal form elements on a line
        • http://sigmajs.org/ les mis tutorial - why are my canvases 0 height?
        • My navbar is not expanding after collapse
        • when a checkbox is checked how to display a different hidden element using javascript
        • Gaps Vertically Using Dividers
        • Svg containers not positioning properly

        Related Questions in REPLACE

        • String.replace() isn't working like I expect
        • String replace with integer not working
        • Java replace every Nth specific character (e.g. space) in String
        • Replace characters in java string
        • Replace special qoutes with normal
        • JavaScript's document.cookie does not replace cookie in the subdomain
        • .NET Replace() doesn´t work
        • What is a regular expression for inserting a string into a Find and Replace match?
        • RegEx replace returns unexpected result without .*
        • MySQL string quotation for REPLACE()
        • sed: make changes in the same file on Windows
        • Replace multiple values in a variable
        • Return what was replaced in SQL
        • Regex replace linebreak and comma
        • Using R to repeatedly replace words

        Related Questions in ITERATED-FUNCTION

        • OSError - could not get source code, during Pandas rolling apply an iterative function
        • How to create a loop which creates multiple subset dataframes from a larger data frame?
        • Iterate trough a converted datetime pandas dataframe with a external function
        • Split, Sum and Iterate through an Integer Array Until Each Side is Equivalent (Javascript)
        • To iterate all the dataframe in pandas
        • How to debug error this File "compiler.py", line 8 f(n, m+1) = f(n-1, m) + f(n, m) + f(n+1, m) ^ SyntaxError: can't assign to function call
        • creating new arrays based on and ordered by one original array in javascript
        • Python program to calculate voxel count
        • Python - iterating over lists
        • How to apply a function over a list using previous outputs as arguments?
        • How do I compare similar file names with timestamps in the names to see which is the newest in Bash?
        • How can I parse json array from json object?
        • Neo4J - find if value in array property of 2 nodes matches
        • Iterate over <li> and replace text with jquery
        • Python / Updating panda row with new column value from function

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates