I’m trying to figure out how to trigger my jQuery function each time a option is selected in my multiple selectbox?
Here is what i got:
<select id="newbtn" name="test" multiple="multiple">
<?
$str = '<b>This is a test!</b>Why doesnt this WORK!<div>Just testing...</div>';
$results = preg_match_all('~<([^/][^>]*?)>~', $str, $arr);
$arr = array_unique($arr[1]);
foreach($arr as $arr1=>$value){
echo '<option">'.htmlentities('<'.$value.'>').'</option>';
}
?>
</select>
<?
echo '<div id="strDiv">';
echo $str;
echo '</div>';
?>
$('#newbtn').click(function() {
$('#strDiv').each(function() {
var s = '<?=$str;?>',
sel = document.getElementById("tags"),
re;
for (var i = 0, len = sel.options.length; i < len; i++) {
if (sel.options[i].selected) {
re = new RegExp(sel.options[i].text.replace("<", "<\\\\/?").replace(">", "[^>]*?>"), "gi");
s = s.replace(re, "");
}
}
$(this).html(s);
});
});
Nothing happens when a option is selected? If I do it with a button like this:
Do you have a test page that demonstrates the problem, or failing that can you provide us with the HTML code that you’re having the problem with?
PHP code is processed by the server and doesn’t get to the client page where JavaScript runs, so knowing what the HTML code actually is allows us to work out the problem.
$('#tags').click(function() {
$('#strDiv').each(function() {
var s = '<?=$str;?>',
sel = document.getElementById("tags"),
re;
for (var i = 0, len = sel.options.length; i < len; i++) {
if (sel.options[i].selected) {
re = new RegExp(sel.options[i].text.replace("<", "<\\\\/?").replace(">", "[^>]*?>"), "gi");
s = s.replace(re, "");
}
}
$(this).html(s);
});
});
It breaks due to a syntax error, due to the string on the first line not being complete.
You can tell JavaScript that the first line continues by ending that first line with a backslash.