www.webdeveloper.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Posts
    6

    [JavaScript] Delete Buttons Not Working

    Hi everybody!
    I'm new in the forum and new in JavaScript!
    Ok, the problem is the following:
    i need to have a dinamic form where i can add contacts, documents and other stuff, i've figured that out, but i add a delete button (in this case its a div) and when i click it gives an alert telling what ID it has, the problem is that only the first of the button shows the alert.

    Code:
    $(function() {
    $('div.icon').click(function() {
      alert( $(this).attr('id') );
    });
    });
    for example contacts:
    Code:
    <span class="contact">
    <input type="text" />
    <select> <!-- options --> </select>
    <div class="icon" id="delete">
      <img src="delete.png" />
    </div>
    </span>
    and this last code is added when clicked on add button xD (with the correct attributes, like name, values, classes, etc).
    The ID of the icon is the same to figure out what type of icon it is, I've tested with diferent ID's but the result is the same.
    Does anyone have any idea?

    You can test at http://ba.dotslashsource.com

    Any help is welcomed!
    Thanks !

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,427
    Shall we guess which library you have used? Is it JQuery? Is it Prototype? Or?
    Whenever you post a problem which is not related with native JavaScript, please, make clear the library used.

  3. #3
    Join Date
    Apr 2012
    Posts
    6
    Sorry, im using jQuery.

  4. #4
    Join Date
    May 2006
    Location
    Russia, Rostov-on-Don
    Posts
    1,237
    every id attribute must be unique but you have four id="delete"
    use [code]YOUR CODE GOES HERE[/code] or burn in Hell

  5. #5
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by Padonak View Post
    every id attribute must be unique but you have four id="delete"
    Hi mate, thanks for the reply, but i've tested even with diferent id's, and just tested it again, and still only the first one is clickable.
    I've tried the .on() function and it didnt work either.

    Could it be cause of the buttons been added after the script been made?

  6. #6
    Join Date
    May 2006
    Location
    Russia, Rostov-on-Don
    Posts
    1,237
    in Opera all the 4 buttons are clickable and every button alerts "delete". your page has much of js code and it is hard to find bugs. if you put here a simplified version and describe the problem much clearly i hope it is going to be resolved sooner.
    use [code]YOUR CODE GOES HERE[/code] or burn in Hell

  7. #7
    Join Date
    Apr 2012
    Posts
    6

    resolved

    Quote Originally Posted by XicoXperto View Post
    Could it be cause of the buttons been added after the script been made?
    I've tested and found that when i add elements on cliente side i need to execute the call again, so i've added:
    Code:
    $('div.icon').off("click");
    $('div.icon').click(function(){alert( $(this).attr('id') );});
    these 2 lines to code, first one to clean what the "buttons" already had onclick event and then added the event to the buttons

    Thanks for the help!

  8. #8
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by Padonak View Post
    in Opera all the 4 buttons are clickable and every button alerts "delete". your page has much of js code and it is hard to find bugs. if you put here a simplified version and describe the problem much clearly i hope it is going to be resolved sooner.
    Oh didnt test on Opera.
    But it was that what i've wanted to.
    The problem was that when i added the new elements i didnt add the click event to the new elements.


    Code:
    <script language="javascript" >
    // add elements
    $(function() {  
    	$('input[type="button"].plus').click( function () {
    		
    		// get vars
    		var type = $(this).attr("id");
    		var actualPerson = $('input[type="hidden"]#activePerson').val();
    		var newId=  $('input[type="hidden"]#' + type).val();
    		
    		// change for the new ID number and write it at it's input
    		newId++;
    		$('input[type="hidden"]#' + type).val( newId );
    		
    		// finally get the info from PHP 
    		$.post('index.php' , { action : 'new' + type , id : newId } , 
    			function ( output ) {
    				// post it at the right place
    				$('div.blueTopOpen#' + type + 's').find( '.holder#' + type )
    					.append( output.replace( /<% parent %>/g , 'pessoa' + actualPerson ) ); 
    	
    				// transform remaining inputs
    				$("form").removeClass("jqtransformdone");
    				$('form').jqTransform({ imgPath: 'images/img/' });
    
    				// giving Icon the click event
    				$('div.icon').off("click");
    				$('div.icon').click(function(){alert( $(this).attr('id') );});
    			});
    	});
    });
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles