-1

I do have a string that contains keywords, like:

$keywords = "shoes, bags, shirts, accessoires"

I want to have created a list of checkboxes out of this string. The checkboxes should show up after loading the page and all checkboxes should be checked, like:

[x] shoes [x] bags [x] shirts [x] accessoires

So user should be able to uncheck some keywords to narrow down search results.

Is this possible with jquery? Thanks,

1
  • 5
    This is possible with jQuery. Please post the code you have attempted to solve this problem with youself, as per the FAQ. Commented Oct 1, 2013 at 12:12

2 Answers 2

3

split up the keywords, and iterate over them creating a label and a checkbox :

var keywords = "shoes, bags, shirts, accessoires";

$.each(keywords.split(','), function(_, word) {
    var lbl = $('<label />', {text: word}),
        box = $('<input />', {type: 'checkbox', name: word, checked: 'checked'});

    lbl.prepend(box).appendTo('body');
});

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This works great to generate checkboxes. Just don't know how get the $keywords string into jquery as var keywords = $keywords; does not work?!?
0
$keywords = "shoes, bags, shirts, accessoires";

var keyword_array=$keywords.split(",");
var str="";
$.each(keyword_array, function( index, value ) {
str +="<input type='checkbox' >"+ value + "</input>";  

});

now use this str to set html by html

reference each

2 Comments

@Salim in question it is not mention that is is come in php i give my answer by assuming a string which is store in $keywords and we can make same variable in jquery also and thanks for focus
it is possible @Salim see in javascript--->developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.