views:

1451

answers:

1

I'm looking to expand on a recent script i've coded using jquery.

I have this following code

<script type='text/javascript'>
added_departments = new Array();
$("#departments_submit").click(function(){
    var depo = $("#depo_list").val();
    if(jQuery.inArray(depo, added_departments) != -1)
    {
     return false;
    }
    else
    {
     added_departments.push(depo);

        $("#depo_added_list").append("<li>" + depo + "<a href='#' title='"+ depo +"' class='remove_depo'> [X] </a></li>");
        var current_value = $("#departments").val();
     if(current_value)
     {
      $("#departments").val(current_value + "," + depo);
     }
     else
     {
      $("#departments").val(depo);
     }
        return false;
    }
});

</script>

The above code takes information selected in a select drop down box, adds it to a div to display publicly and also into a hidden form field that processes the data.

i've tried to create now something that will reverse this effect and remove certain selections from the div and the field. which is where i have this code

<script type='text/javascript'>
$(".remove_depo").click(function(){


var removing = $(this).title();
var current_val = $("#deparments").val();
if(current_val == removing) {
    $("departments").replace(removing, "");
}
else {
    $("departments").replace("," + removing, "");
}


});
</script>

It doesn't cause any errors, but it doesn't do anything either? So I'm really stuck. Any ideas?

EDIT: Updated code

$(".remove_depo").click(function(){


var removing = $(this).attr('title');
var current_val = $("#deparments").val();
if(current_val == removing) {

 $("#departments").replace(removing, "");

}
else {

 $("#departments").replace("," + removing, "");

}


});

Here is the html

<form method="post" action="javascript:void(0);">Select Departments To Be Added: 
 <div class="depo_adder">
  <select id="depo_list"><option value="">--- INDIVIDUAL TAGS ---</option><option value="blah">blah</option></select>

  <button id="departments_submit">Go!</button>
 </div></form><form method="post" action="briefings/addbriefing.php">

 <div class="form">
  <strong>Departments: </strong>
  <ul id="depo_added_list"><li>blah<a href="#" title="blah" class="remove_depo"> [X] </a></li></ul>
  <input name="departments" id="departments" value="blah" type="hidden">

 </div>
+4  A: 

you're referring to $('departments') - this won't work. You need to specify either an identifierm eg $('#departments') or a class, eg $('.departments)

ah - other answer is also correct, .title() is not a function. You want

$('#foo').attr('title') to get the title.

jan
ok i've changed the code slightly... however it still doesn't seem to work as required.
Neil Hickman