Javascript Course

The keepFirstImgs() function presented in this page can be used in JavaScript to keep the first Nr IMG tags into a string, and Remove all the other <img>s.
- The function receives the number of the first <img> tags to keep (nrimg), and the string with the HTML text (str); returns the string with the other IMG tags removed.
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}
- Example:
<script type="text/javascript">
// Function to keep first $nrimg IMG tags in $str, and removes all the other <img>s
// From: https://coursesweb.net/javascript/
var keepFirstImgs = function(nrimg, str) {
  // gets array with the <img>s that must be stripped from $str ($nrimg+), and removes them
  var remove_imgs = str.match(/(\<img[^\>]+\>)/ig).slice(nrimg);
  var nr_i = remove_imgs.length;
  for(var i=0; i<nr_i; i++) str = str.replace(remove_imgs[i], '');
  return str;
}

// Test, keeps the first two IMG tags in $str
var str = 'First img: <img src="img1.jpg" alt="img 1" width="30" />,\
 second image: <img src="img_2.jpg" alt="img 2" width="30">,\
 another Img tag <img src="img3.jpg" alt="img 3" width="30" />,\
 and another image <img src="img_4.jpg" alt="imgage 4" width="30" /> ... etc.';

str = keepFirstImgs(2, str);

alert(str);
/* Result:
 First img: <img src="img1.jpg" alt="img 1" width="30" />,
 second image: <img src="img_2.jpg" alt="img 2" width="30">,
 another Img tag , and another image  ... etc.
*/
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Keep the first Nr IMG tags, Remove all the others

Last accessed pages

  1. Integer and Float value in Select with PDO from string to numeric (8672)
  2. Get and change IFrame content through a JavaScript script created in another IFrame (16553)
  3. Shape Tween - Flash Animation (6185)
  4. CSS Border (6122)
  5. Disable button and Enable it after specified time (17587)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (524)
  2. CSS cursor property - Custom Cursors (70)
  3. The Mastery of Love (50)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (48)
  5. Read Excel file data in PHP - PhpExcelReader (46)