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?

Priya
  • 1,453
  • 4
  • 29
  • 55
  • selectedElem is printed correct but elements are not hidden with $(selectedElem).hide(). is there anything wrong related to concatenation? – Priya May 21 '14 at 03: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 http://jsfiddle.net/rbGfg/ – caspian May 21 '14 at 04:10

3 Answers3

1

Using jquery:

$('a').filter(function(){
    return this.name === '3257';
}).hide();
Jai
  • 74,255
  • 12
  • 74
  • 103
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;
}
amunozdv
  • 111
  • 3
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>
caspian
  • 1,804
  • 14
  • 14