0

I am using backbone and

I have following code in template

    <a class="al_ynbtn apv_app" id="approveLeave" name=<%=leave_request_id%>></a>
    <a class="al_ynbtn can_app" id="rejectLeave" name=<%=leave_request_id%>></a>

and in render function i have following code

  render: function() {
        $(this.el).html(this.template(this.model));
        var selectedElem='[name='+self.model.leave_request_id+']';
            console.log(selectedElem);
            console.log($(selectedElem));
            //$("a[name='"self.model.leave_request_id+"']" )
            $(selectedElem).hide();


        return this.el;
    }

console.log(selectedElem) prints [name=3257]

and console.log($(selectedElem)) prints

[a#approveLeave.al_ynbtn.apv_app, a#rejectLeave.al_ynbtn.can_app, prevObject: m.fn.init[1], context: document, selector: "[name=3257]", jquery: "1.11.1", constructor: function…] 0: a#approveLeave.al_ynbtn.apv_app 1: a#rejectLeave.al_ynbtn.can_app context: document length: 2 prevObject: m.fn.init[1] selector: "[name=3257]" proto: Object[0]

i want to hide elements with name=3257? how to do that?

2
  • selectedElem is printed correct but elements are not hidden with $(selectedElem).hide(). is there anything wrong related to concatenation?
    – Priya
    Commented May 21, 2014 at 3:54
  • Not sure exactly what is wrong - I think there is an issue elsewhere in your code. I simplified the code to just query by the name and it works fine. see jsfiddle.net/rbGfg
    – caspian
    Commented May 21, 2014 at 4:10

3 Answers 3

1

Using jquery:

$('a').filter(function(){
    return this.name === '3257';
}).hide();
1
render: function() {
    $(this.el).html(this.template(this.model));
    var selectedElem='[name='+self.model.leave_request_id+']';
    //$('[name=\'3257\']').hide(); //Hardcoded name value
    $('[name=\'' + self.model.leave_request_id + '\']').hide();//jQuery cascades so you can call .hide() on the same line
    return this.el;
}
0

I think you're missing quotes around your names:

<a class="al_ynbtn apv_app" id="approveLeave" name="<%=leave_request_id%>"></a>
<a class="al_ynbtn can_app" id="rejectLeave" name="<%=leave_request_id%>"></a>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.