16192

get value of sibling in same div

Question:

I would like to do a certain kind of logic for every item with a given class (the code is reused that why i dont work with id): the items are within the same div everywhere:

<div id="mDiv> <input type="text" class = "class1> <input type="hidden"/> </div>

and the script is something like that:

$(".class1").val(calcDefaultDate()) function calcDefaultDate(){ var currDate = $(this).siblings('input[type=hidden]').val(); if (currDate == "") { return new Date(); } return currDate; }

The problem is that this way the $(this) is the Window instead of the element with class1, I am quite new to javascript so it might be something really easy but how should it be handled?

Thanks!

Answer1:

You are very close, You just need to pass the function reference like

$(".class1").val(calcDefaultDate); //Notice removed ()

When you use () one is calling function.

<a href="http://jsfiddle.net/satpalsingh/9koncuvz/" rel="nofollow"><strong>DEMO</strong></a>

Answer2:

You can try to give the element as an argument instead of trying to reach it with the this keyword:

$(".class1").val(calcDefaultDate($(".class1"))) function calcDefaultDate($element){ var currDate = element.siblings('input[type=hidden]').val(); if (currDate == "") { return new Date(); } return currDate; } <hr />

By the way, maybe you would prefer return new Date(+currDate); instead of return currDate;, this way the type of what your function returns is more consistent.

Answer3:

There need to be some change in implementation.

Do it like bellow

$(".class1").each(function(){ var currDate = $(this).siblings('input[type=hidden]').val(); if (currDate == "") { $(this).val(new Date()); } else $(this).val(currDate); });

<a href="http://jsfiddle.net/mkdskd/h9306qq5/2/" rel="nofollow"><strong>DEMO</strong></a>

Answer4:

Try passing the div as an argument to your function:

$(".class1").each(function() { $(this).val(calcDefaultDate($(this))); }); function calcDefaultDate(el) { var currDate = $(el).siblings('input[type=hidden]').val(); if (currDate == "") { return new Date(); } return currDate; }

Or apply the function to each element:

$(".class1").each(function() { updateDefaultDate($(this)); }); function updateDefaultDate(el) { var currDate = $(el).siblings('input[type=hidden]').val(); if (currDate == "") { currDate = new Date(); } el.val(currDate); }

Or pass all the elements to your function:

updateDefaultDate($(".class1")); function updateDefaultDate(elements) { $(elements).each(function() { var currDate = $(this).siblings('input[type=hidden]').val(); if (currDate == "") { currDate = new Date(); } $(this).val(currDate); }); }

Recommend

  • How to display formatted HTML data in kendo ui grid column
  • Deserialize array of objects inside another object using Gson
  • Fork/Join kill siblings/branches
  • h1, h2, h3.. elements eats div margins
  • Fading between images
  • Jquery Mobile + Phonegap improve listview performance
  • Pass a custom comparator through a function
  • Cannot change class variables with multiprocessing.Process object in Python3
  • insert radio value multiple data codeigniter in database to one row?
  • Universal Image Loader reuse images
  • Group list of tuples by item
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • jQuery .attr() and value
  • DomPDF {PAGE_NUM} not on first page
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Illegal mix of collations for operation for date/time comparison
  • Javascript simulate pressing enter in input box
  • MySQL WHERE-condition in procedure ignored
  • Importing jscolor library in angular 2
  • what is the difference between the asp.net mvc application and asp.net web application
  • Release, debug version and Authorization Google?
  • Web-crawler for facebook in python
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • trying to dynamically update Highchart column chart but series undefined
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?