SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  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,684
    Mentioned
    99 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,684
    Mentioned
    99 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...

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jmansa View Post
    One more question though...

    If I break my $str by hitting enter somewhere in the text it wont work??? Any ideas!
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    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.
    Oh yes, but what if I get the text from something I don't have controll over?

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jmansa View Post
    Oh yes, but what if I get the text from something I don't have controll over?
    Then you need to exert control over it, via I suspect some appropriate PHP coding.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Then you need to exert control over it, via I suspect some appropriate PHP coding.
    If I insert a "\" in my php text it will show the backslash? Is there a way to check for syntax errors in javascript and correct it here?

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jmansa View Post
    If I insert a "\" in my php text it will show the backslash? Is there a way to check for syntax errors in javascript and correct it here?
    Not that I know of, no.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found the solution...

    PHP Code:
    $str str_replace("\n",""$str);
    $str str_replace("'","\'"$str); 
    Thanks a lot.


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
  •