0

I have a set of checkboxes with span elements as their titles. I want the text in the span to wrap with a url once the checkbox is checked. However, I do have the code down for selecting the span class names. In order for my code to work I have my checkbox id the same name as my span. This does work if I set the array myself but for some reason I cannot get my array to work when it is created with the script. Here's the code

HTML:

<div id="product_list" style="float:left; clear:right;">
<input type="checkbox" id="p01" name="system[]" value="p01" form="compare">
<span style="margin-left:20px;" class="p01">product one</span>
<br/>

<input type="checkbox" id="p02" name="system[]" value="p02" form="compare">
<span style="margin-left:20px;" class="p02">product two</span>
<br/>

<input type="checkbox" id="p04" name="system[]" value="p04" form="compare">
<span style="margin-left:20px;" class="p04">product three</span>
<br/>

<input type="checkbox" id="p05" name="system[]" value="p05" form="compare">
<span style="margin-left:20px;" class="p02">product four</span>
<br/>

</div>

JQUERY:

var arr = [];
arr = $('#product_list span').not('.hidden').map(function(i,e) {
return $(e).attr('class');
}).get().join(', ');

alert(arr);

    //arr = ["p01", "p02", "p03", "p04"]; works but not the above.

jQuery.each(arr , function(index, value){

$("#"+ value).click(function(){
// alert("clicked");
if ($(this).attr("checked") == "checked"){
$('.'+value).wrap('<a href="#" id="comparelink" target="_blank"></a>'); $('a').link('href', '#');    
}

else {
$('.'+value).unwrap('a');

}

});

});

example

4 Answers 4

1

Right now, your code is setting arr to a string as follows

"p01, p02, p04, p05"

Simply, remove the String#join to generate the array you want.

var arr = $('#product_list span').not('.hidden').map(function(i, e) {
    return $(e).attr('class');
}).get();

Furthermore, jQuery has no method .link(). I suppose you meant to use .attr().

Use

$('a').attr('href', '#'); 

instead of

$('a').link('href', '#'); 

I applied some modifications to your code.

  1. Usage of change event handler.
  2. Cache of selector, such as $span and $input.
  3. Usage of .prop() instead of .attr().
  4. Usage of .prev() to get the input elements since all of them happen to be just before your span elements.
  5. Change from id attribute to class attribute since ID are meant to be unique over all the DOM.

$('#product_list span').not('.hidden').each(function() {
    var $span = $(this), $input = $span.prev();
    $input.change(function() {
        if ($input.prop("checked")) {
            $span.wrap($('<a/>', {
                "href": "#",
                "class": "comparelink",
                "target": "_blank"
            }));
        } else {
            $span.unwrap('a');
        }
    });
});​

See it here.

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

5 Comments

thank you Alexander and @charlietfl. Worked awesome. Also thank you pointing out the link too
$('a').attr('href', '#'); will change every <a> in page... no idea what is wanted
@charlietfl, neither me. I suppose was meant to be completed afterwards
@Far, if you are open to suggestions, feel free to see my updated answer
thank you, it was included just to show what the intention of making the array was for. I really wasn't looking for a fix on the link thinking I didn't need to but you proved me wrong :-) Thank you again very much for helping me out on that. I'll read up more on the documentation of the prop and prev
1

Just remove the join().

arr = $('#product_list span').not('.hidden').map(function(i,e) {
return $(e).attr('class');
}).get();

get() unwraps the array from jQuery object

You could do this without using array, rather using simple traverse.

var $checkboxes = $('#product_list span').not('.hidden').prev();
    $checkboxes.click(function(){
         if( this.checked){
              /* "A" tag has problems...can't duplicate ID's using class and no idea what href you want*/              
              $(this).next('span').wrap('<a href="#" class="comparelink" target="_blank"></a>'); 
         }else{
              $(this).next().find('span').unwrap()
         }
    });

Comments

0

You're not really building an array but a string ("p01, p02, p03, p04").

Replace

var arr = [];
arr = $('#product_list span').not('.hidden').map(function(i,e) {
return $(e).attr('class');
}).get().join(', ');

with

var arr = $('#product_list span').not('.hidden').map(function(i,e) {
    return $(e).attr('class');
});

1 Comment

Thank you, I almost figured that, lol but when I run this script it gives me [object Object]
0

This is more of an alternative answer because it doesn't really address the array issue, but why do you need to use an array? Would this work for your code, it grabs all the input elements with a name of system[] and assigns the click event:

$('input[name="system[]"]').on('click', function() {
    var spanClass = $(this).attr('id');
    if ($(this).attr("checked") == "checked") {
        $('.' + spanClass).wrap('<a href="#" id="comparelink" target="_blank"></a>');
    }
    else {
        $('.' + spanClass).unwrap('a');
    }
});

​ You could also give those inputs a shared class like 'chkLink' and use that in place. Just seems like you're doing something really difficult for something that could be simplified... either that or I don't grasp the full intent of your application.

Comments

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.