0

I have a list control and at run time when I bind data to the control I want to append a delete icon or a button to each row in jquery, so that I can delete a row if I want to. Here is the code that I am using to bind data to the control.

$(response.aaData).each(function (index, val) { 
    $("#multiselectSubCat")
        .append($('<option></option>').val(val.SubCategoryId).html(val.SubCategoryName)); 
});

Rendered

<select name="from" id="multiselectSubCat" multiple="multiple" style="width: 300px; top: 100px">
 <option value="9">Category1</option>
 <option value="10">Category2</option>
 <option value="11">Category3</option>
 <option value="12">Category4</option>
 <option value="13">Category5</option>
 <option value="22">Category6</option>
</select>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Gaurav Sachdeva
  • 75
  • 2
  • 3
  • 9

3 Answers3

0

I want to know whether you want input button or image to show user that you can delete particular record?

I have made an example where I am adding background.

.select-box {
  height: 400px;
}
.select-box option {
  background: url(https://cdn2.iconfinder.com/data/icons/snipicons/500/minus-sign-16.png) 1px -1px no-repeat;
  padding-left: 25px;
  cursor: pointer;
}
.select-box option:hover {
  background: url(https://cdn2.iconfinder.com/data/icons/snipicons/500/minus-sign-16.png) 1px -1px no-repeat #eee;
}
<select name="from" id="multiselectSubCat" multiple="multiple" class="select-box" style="width: 300px; top: 100px">
  <option value="9">Category1</option>
  <option value="10">Category2</option>
  <option value="11">Category3</option>
  <option value="12">Category4</option>
  <option value="13">Category5</option>
  <option value="22">Category6</option>
</select>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ajay Malhotra
  • 798
  • 4
  • 16
  • 1
    this is more a comment than an actual answer – maRtin Jun 12 '15 at 10:39
  • Hi.. Thank you..Yes this is probably what I want the list to look like. I want to add an image (cross sign). – Gaurav Sachdeva Jun 12 '15 at 11:17
  • You can add the icon to the select, but you cannot see if the user clicked the delete or just selected the option – mplungjan Jun 12 '15 at 11:44
  • Hi mplungjan That You can do using javascript. and Gaurav just change the background image link in css if u want to change image – Ajay Malhotra Jun 12 '15 at 11:48
  • No. You cannot click on the icon in a dropdown and see you clicked the icon part of the background – mplungjan Jun 12 '15 at 11:50
  • Ya ryt u cant click on image, u r clicking on option tag full – Ajay Malhotra Jun 12 '15 at 11:52
  • In Javascript using value of option tag u can delet particular record – Ajay Malhotra Jun 12 '15 at 11:53
  • what do u want exactly? do u want input type="button" ? – Ajay Malhotra Jun 12 '15 at 11:54
  • @user3233135 : I think it wont serve any purpose if we cannot identigy whether user clicked on image or the row. This is because on click of a row I am using another event which populates data in another control. yes, the alternate option could be a button. Can we add a button to the same? (But an image would have been a better option if it were possible) – Gaurav Sachdeva Jun 12 '15 at 12:10
0

Building on this How can I use <ul> list instead of <select> dropdown for the languages switcher?

have a go at this:

var nav = $('#nav');
var selection = $('.select');
var select = selection.find('li');

nav.click(function(event) {
    if (nav.hasClass('active')) {
        nav.removeClass('active');
        selection.stop().slideUp(200);
    } else {
        nav.addClass('active');
        selection.stop().slideDown(200);
    }
    event.preventDefault();
});

select.click(function(event) {
    // updated code to select the current language
    select.removeClass('active');
    $(this).addClass('active');

    alert ("location.href = 'index.php?lang=" + $(this).attr('data-value'));
});

$(".del").on("click",function(e) {
  e.preventDefault();
  $(this).parent().remove();
});  
h2 {
    width: 200px;
    background: #222;
    color:  #eee;
    line-height: 25px;
    font-size: 14px;
    padding: 0 10px;
    cursor: pointer;
}

ol
{
    list-style-type: none;
}


ol.select {
    display: none;
}

ol.select > li {
    width: 200px;
    background: #eee;
    line-height: 25px;
    font-size: 14px;
    padding: 0 10px;
    cursor: pointer;
}

ol.select > li:hover {
    background: #aaa;
}
.select a { text-decoration:none }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h2 id="nav">Choose Language</h2>
<ol class="select">
    <li data-value="en"><a class="del" href="#">X</a> English</li>
    <li data-value="de"><a class="del" href="#">X</a> Deutsch</li>
</ol>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

U did the right thing just little changes required

<a href="http://jsfiddle.net/ajaymalhotra15/2tmntdft/" > click here to see code </a>
Ajay Malhotra
  • 798
  • 4
  • 16