Jump to content

Javascript Guru Need - How to Select all instances of same CSS class?


soma56

Recommended Posts

Here's an example of what I've been banging my head against the wall with for the last couple of days. If you throw it in a browser it will make sense. 

[code=php:0]
<script type="text/javascript">
function fnSelect(objId)
{
   fnDeSelect();
   if (document.selection) 
   {
      var range = document.body.createTextRange();
      range.moveToElementText(document.getElementById(objId));
      range.select();
   }
   else if (window.getSelection) 
   {
      var range = document.createRange();
      range.selectNode(document.getElementById(objId));
      window.getSelection().addRange(range);
   }
}
function fnDeSelect() 
{
   if (document.selection)
             document.selection.empty();
   else if (window.getSelection)
              window.getSelection().removeAllRanges();
} 
</script>


<h1>How Do I Only Highlight the Numbers?</h1>
<form>
<input type="button" value="Select/Highlight" onClick="fnSelect('mydiv')">


<?PHP
   

$i = 0; 

while ($i <= 100){
ob_flush();
flush();
$i++;
echo "<div class=\"my class\" id=\"mydiv\">$i</div>";
echo "<br />";
echo "<div id=\"anotherdiv\">I Like Numbers</div>";
echo "<br />";

// wait for 1 second
usleep(1000000);

}

?>
</form>

 

 

My php script echoes out the div's I need highlighted along with other information in chunks. I need to be able to click the button and highlight the same css class that is on the page.

Link to comment
Share on other sites

Sure:

 

<h1>How Do I Highlight All The Numbers?</h1>
<form>
<input type="button" value="Select/Highlight" onClick="fnSelect('mydiv')">


<div class="my class" id="mydiv">1</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">2</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">3</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">4</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">5</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">6</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">7</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">8</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">9</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">10</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">11</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">12</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">13</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">14</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">15</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">16</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">17</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">18</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">19</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">20</div><br /><div id="anotherdiv">I Like Numbers</div><br /><div class="my class" id="mydiv">21</div><br /><div id="anotherdiv">I Like Numbers</div>
</form>

 

The php script runs through a simple loop echoing out numbers. While the button can highlight the first number I'm trying to figure out how to highlight all instances of the same div. I think it can only be done through a class or name associated with the div.

 

While I did figure out how to actually highlight the div:

 

<script type="text/javascript">
function highlight (item) {
var items = document.getElementsByName (item);
var j=items.length;
for (var i=0; i<j; i++) {
	items[i].className = "highlight";
}
}
</script>
<style type="text/css">
.highlight {
background: #FF0000;
color: #0000FF;
}
</style>
<h1>This is one way to highlight the target DIVs</h1>
<form>
<input type="button" value="Select/Highlight" onClick="highlight('thisone')"><br><br>

<?php
$i = 0; 

while ($i < 20){
$i++;
echo '<div name="thisone">Highlight this DIV - '.$i.'</div>';
echo '<br />';
echo '<div>I Like Numbers</div>';
echo '<br />';
}
?>
</form>

I couldn't figure out how to highlight them as a means of 'selecting' the text for copying. I.E. hit CTRL-C right after highlighting to copy. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.