SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery: Trigger jQuery on select from multiple selectbox?

    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:
    PHP Code:
    <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>';
    ?>
    Code JavaScript:
    $('#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:

    <input type="button" value="Go Jquery" id="newbtn"/>

    Then it works, but I would like to do it without the use of a button...

    Thanks in advance ;-)

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jmansa View Post
    I'm trying to figure out how to trigger my jQuery function each time a option is selected in my multiple selectbox?
    Try using the change event instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Try using the change event instead.
    Tryied this but no luck:

    $('#strDiv').change(function() {

    Did you meen something else?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jmansa View Post
    Tryied this but no luck
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ups... Got it working.

    My selectbox looks like this
    Code:
    <select id="tags" name="test" multiple="multiple">
    And then the jQuery looks like this:

    Code JavaScript:
    $('#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);
    	});
    });

    The issue was the first line in the jQuery:
    Code:
    $('#tags').click(function() {
    I had #newbtn instead of #tags.

    Thanks al ot for your help ;-)

  6. #6
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more question though...

    If I break my $str by hitting enter somewhere in the text it wont work??? Any ideas!

    Working $str:
    PHP Code:
    $str '<b>This is a test!</b>Why doesnt this WORK!<div>Just testing...</div>'
    This dont work?:
    PHP Code:
    $str '<b>This is a test!</b>Why doesnt this WORK!
        <div>Just testing...</div>'

    Please help...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •