locked
If statement not evaluating correctly? RRS feed

  • Question

  • I have the script below in which I am assigning the currently selected value of a dropdown field to a variable. I am confirming the value of the variable in a console.log statement. In an IF statement I then evaluate if the variable but even though the Log statement confirms that it should evaluate to the true part of the IF statement, it evaluates to the false part of the IF statement.

    Any ideas?

    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" id="ShowHideConfirmButton">
    	
    	function DoIt() {
    		var DropdownValue = $("select[title='Status'] option:selected").val()
    		console.log("Dropdown Value: "+DropdownValue);
    		if (DropdownValue == "Live") {
    			console.log("It's Live");
    		} {
    			console.log("It's NOT Live");
    		}
    	}
    	
    	_spBodyOnLoadFunctionNames.push("DoIt");
    	
    </script>

    Monday, January 8, 2018 2:29 PM

Answers

  • Hi,

    Modify the code as below:

    <script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
    $(function () {
    	DoIt();
    });	
    function DoIt() {
    	var DropdownValue = $("select[title='Status'] option:selected").val()
    	console.log("Dropdown Value: "+DropdownValue);
    	if (DropdownValue == "Live") {
    		console.log("It's Live");
    	}else{
    		console.log("It's NOT Live");
    	}
    }	
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, January 9, 2018 3:12 AM

All replies

  • Hi,

    try this

    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" id="ShowHideConfirmButton">

    function DoIt() {
    var DropdownValue = $("select[title='Status']").val();
    console
    .log("Dropdown Value: "+DropdownValue);
    if (DropdownValue == "Live") {
    console
    .log("It's Live");
    } {
    console
    .log("It's NOT Live");
    }
    }

    _spBodyOnLoadFunctionNames
    .push("DoIt");

    </script>

    Monday, January 8, 2018 5:52 PM
  • Hi,

    Modify the code as below:

    <script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
    $(function () {
    	DoIt();
    });	
    function DoIt() {
    	var DropdownValue = $("select[title='Status'] option:selected").val()
    	console.log("Dropdown Value: "+DropdownValue);
    	if (DropdownValue == "Live") {
    		console.log("It's Live");
    	}else{
    		console.log("It's NOT Live");
    	}
    }	
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, January 9, 2018 3:12 AM