-2

I'm trying to get department's id value. I mean I need to get the departmentID from the element.

This function is getting the input value of elements having departmentName as a class.

this.$el.find(".departmentName").val()

example jSON

{
  "departmentID": 1,
  "departmentName": "BT"
},

My code from a BackboneJS view callback

saveEvent:function(e){
  this.$el.find(".departmentName").val(); // getting "BT"
  this.$el.find(".departmentName").attr("departmentID");//this is not working

  this.render();
},

template

<th>
    <span>{{user.department.departmentName}}</span>
    <input data-placement="top" 
           data-id="{{user.department.departmentID}}" 
           title="Boş geçilemez!" 
           class="form-control text-center departmentName" 
           type='text'
           style='display: none;' 
           value='{{user.department.departmentName}}'/>
</th>
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
real
  • 53
  • 1
  • 2
  • 9
  • 1
    `this.$el.find(".departmentID").val()` or with your update... `this.$el.find(".departmentName").attr("data-id")` ...since that's the name of the attribute. –  Apr 23 '17 at 22:52
  • What is the html fragment like -- its like you are finding the DOM ".departmentName" --- try this - console.log("el", this.$el.find(".departmentName")) –  Apr 23 '17 at 22:52
  • 1
    Your question isn't clearer. From what markup you get attributes? – Pavlo Zhukov Apr 23 '17 at 22:53
  • var el = this.$el.find(".departmentName"); -- then try this console.log("el", el.data()) –  Apr 23 '17 at 22:53
  • I would do a console.log("e", this.$el.data()) --- –  Apr 23 '17 at 22:54
  • 1
    Looking only your JSON it looks like right answer must be `this.$el.find(".departmentID").val();` – Pavlo Zhukov Apr 23 '17 at 22:54
  • sorry wrong failure edited. this.$el.find("departmentName").val() this function getting value of departmentName class – real Apr 23 '17 at 22:57
  • what, exactly is `$el` and where does it come from? Please post a [mcve] – Tibrogargan Apr 23 '17 at 22:57
  • Please don't try to mix jQuery and Angular, this will avoid a lot of headaches... – jcaron Apr 23 '17 at 22:59
  • None of the two lines starting with `this.$el` actually _do_ anything. You’re (possibly) retrieving two values but you’re not actually _doing_ anything with them; you’re not saving them, you’re not returning them, you’re not assigning anything. You’re basically discarding both of them right away. How should they be working, then? – Sebastian Simon Apr 23 '17 at 23:01
  • Possible duplicate of [How to get the data-id attribute?](http://stackoverflow.com/questions/5309926/how-to-get-the-data-id-attribute) – Emile Bergeron Apr 24 '17 at 15:30
  • @Tibrogargan `$el` is a property from a Backbone view. It's just a jQuery element. – Emile Bergeron Apr 24 '17 at 15:33
  • @EmileBergeron you assume it's coming from Backbone. While that is probably a safe assumption it may not be correct (plus if you're going to make the assumption you should have added the tag when you edited it). Read the rest of my comment. The code supplied is not even close to a reproducible case. – Tibrogargan Apr 24 '17 at 17:13
  • @Tibrogargan I was just informing you, but the question is definitely a duplicate and unrelated to Backbone (which is why I didn't added the tag). It's just a poor example to show without any context from OP. – Emile Bergeron Apr 24 '17 at 17:16

1 Answers1

2

Use

this.$el.find(".departmentName").data("id");
Ovais
  • 374
  • 2
  • 7