Forum Moderators: open
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<style type="text/css">
p { font-weight:bold; }
div { width: 20%; margin: auto; }
</style>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(function(){
$('.addit').click(function () { addText($(this).html()); return false;});
});
//
function addText(char1) {
var debug = '';
var char2;
$('#change-graphs p').each(function (i) {
char2 = $(this).attr('class').charAt(1);
debug += 'i: ' + i + 'char: ' + char2 + '\n';
if (char2==char1) { $(this).append('etc etc'); }
});
}
</script>
</head>
<body>
<div id="change-graphs">
<p class="a1">a1</p>
<p class="a2">a2</p>
<p class="a3">a3</p>
<p class="b1">b1</p>
<p class="b2">b2</p>
<p class="b3">b3</p>
</div>
<div>
<p><a href="#" class="addit">1</a></p>
<p><a href="#" class="addit">2</a></p>
<p><a href="#" class="addit">3</a></p>
</div>
</body>
</html>
If I have a collection of <p> tags, and I want to operate on them depending on their class (eg class="a1", "a2" ... "a66", "b1" ,b2... "b66") how to make the class the variable?
The function isn't called from a link (this is where I'm having trouble with your solution), but as part of a sequence of functions within a switch function; and there are multiple similarly-structured switch functions.
function doSome(clss) {
//...
var clss = elP.className;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<style type="text/css">
p { font-weight:bold; }
div { width: 20%; margin: auto; }
</style>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(window).load(
function() {
var test_array = ['a2','b1','b3'];
for (v in test_array) {
addText(test_array[v]);
}
});
//
function addText(char1) {
$('#change-graphs p').each(function (i) {
if ($(this).hasClass(char1)){ $(this).append('etc etc'); }
});
}
</script>
</head>
<body>
<div id="change-graphs">
<p class="a1">a1</p>
<p class="a2">a2</p>
<p class="a3">a3</p>
<p class="b1">b1</p>
<p class="b2">b2</p>
<p class="b3">b3</p>
</div>
<p>This is to demonstrate the function,
<a href="#" onclick="addText('a1'); return false;">Click to append to a1</a></p>
</body>
</html>