1

Here is my script

<input type="text" id="teamleader-default" value="${foo.teamLeaderId}/${foo.teamLeaderFirstName} ${foo.teamLeaderLastName}" />

<script type="text/javascript">
$(document).ready(function(){ 
var teamleader = $('#teamleader-default').val();
var tl_details = teamleader.split("/");

$('#teamleader-default').prop('type', 'hidden');

$("#update").submit(function() {

    if ($('#ParentDD :selected').text() == "Engineer") {
        var tmp0 = $('#teamleader').val(tl_details[1]);
        var tmp1 = $('#teamleaderId').val(tl_details[0]);
        alert(tmp0);
        alert(tmp1);
    } else {
        var tmp2 = $('#teamleader').val('');
        var tmp3 = $('#teamleaderId').val(0);
        alert(tmp2);
        alert(tmp3);
    }
});
});
</script>

The problem is that I can't assign a value for both the teamleader and the teamleader ID

        $('#teamleader').val(tl_details[1]);
        $('#teamleaderId').val(tl_details[0]);

instead the value it contains is just saying "Object object" like the one in this image:

enter image description here

Can anyone help me on how to assign a value for the team leader.

Again a big thanks for those who could help me.

dotvav
  • 2,808
  • 15
  • 31
Mikael1
  • 45
  • 2
  • 11
  • a tip for you, in the future try to simplify the problem (question) and make more generic, this will help you to reach more people and reaching more people get easier to get a answer. – Jonny Piazzi Aug 28 '15 at 13:51
  • Thanks Jon, will surely remember that tip – Mikael1 Aug 28 '15 at 14:04
  • What does `tl_details` log in the console? – somethinghere Aug 28 '15 at 14:13
  • 1
    What do you get if you `console.log(tl_details)`? – Vucko Aug 28 '15 at 14:13
  • 2
    Your value is an object. When put in an alert box like that, it is converted to a string which simply tells you it was an object. Instead, try `console.log(tmp0)` and you can inspect it in the browser's developer tools. – dsh Aug 28 '15 at 14:14
  • What do You get for tl_details? – dansasu11 Aug 28 '15 at 14:15
  • 2
    from [docs](http://api.jquery.com/val/) `val(avalue)` return jQuery Object, not the value setted . it allow chaining function call on the same jQuery Object – Hacketo Aug 28 '15 at 14:16
  • @Hacketo Good catch. – somethinghere Aug 28 '15 at 14:16
  • @somethinghere, only if used as getter, i.e. without params :-) – Grundy Aug 28 '15 at 14:17
  • @somethinghere @Vucko @dansasu11 the value i get from `console.log(tl_details)` is `["1", "Raymond Esguerra"]` @dsh the value i get from `console.log(tm0)` is `[context: document, selector: "#teamleader"]` – Mikael1 Aug 28 '15 at 14:38

1 Answers1

2

I think the comments above have already answered the question quite well, if you want to know whether it is assgined successfully, you can use tm0.val().

By the way, using alert is not my choice to debug code like Js. Since you have chosen Chrome, you can get the advanced debug tool just press F12.You can get some tips on How to use Chrome DevTools

Happy coding~

teik
  • 508
  • 4
  • 26
  • Its true, though it might be better to elaborate a bit on the fact that `val(arg)` does _not_ return the value but an object, which explains the `[object Object]` Also it wouldnt be bad to mention the commenter: Hacketo. – somethinghere Aug 28 '15 at 14:57
  • @somethinghere Got it. Thanks. – teik Aug 28 '15 at 15:02
  • @somethinghere how can i send the values of tl_details[0] and tl_details[1] to a input type hidden? – Mikael1 Aug 28 '15 at 15:37
  • @Mikael1 Just use `val()` in the same way. See this question if you are wondering: http://stackoverflow.com/questions/5289397/jquery-change-value-of-hidden-input-field – somethinghere Aug 28 '15 at 16:00