Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery $(this).val() not working

I am having issues using jquery to set the value a element which calls a function. Here is the code

Element:

 each outlet in FBoutlets  
        br
        label(for='FBoutlet') Facebook: #{outlet.FBoutlets.name}  
        input(type='checkbox', value='', onchange="streamFB(" + outlet.FBoutlets.FBpageId + "," + JSON.stringify(outlet.FBoutlets.FBaccessToken) + ")", name='FBoutletCredentials', id='FBoutlet')

Function:

  script.
    window.fbAsyncInit  = function()  {
    FB.init({
      appId             : '20212222222',
      autoLogAppEvents  : true,
      xfbml             : true,
      version           : 'v2.12'
      });
      };

    function streamFB(pageId, accessToken){
      FB.api(
        '/' + pageId + '/live_videos',
        'POST',
        {access_token: accessToken},
        function(response) {
            let rtmp = response.stream_url
            console.log(rtmp)
            console.log(response)
            $(this).val() = rtmp;
        }
      );
    }

$(this).val() is returning undefined, I need to use this because the element id dynamically generated

like image 447
Ricky Avatar asked Oct 19 '25 09:10

Ricky


2 Answers

You should change it

$(this).val() = rtmp;

to

$(this).val(rtmp);
like image 101
lucky Avatar answered Oct 21 '25 22:10

lucky


jquery val function takes argument value as assigning value

Definition and Usage

The val() method returns or sets the value attribute of the selected elements.

When used to return value:

This method returns the value of the value attribute of the FIRST matched element.

When used to set value:

This method sets the value of the value attribute for ALL matched elements.

Note: The val() method is mostly used with HTML form elements.

Syntax

Return the value attribute:

$(selector).val()

Set the value attribute:

$(selector).val(value)

Set the value attribute using a function:

$(selector).val(function(index,currentvalue));

Parameter Description

value Required. Specifies the value of the value attribute function(index,currentvalue) Optional. Specifies a function that returns the value to set. index - Returns the index position of the element in the set currentvalue - Returns the current value attribute of selected elements

source:https://www.w3schools.com/jquery/html_val.asphttps://www.w3schools.com/jquery/html_val.asp

like image 37
jasinth premkumar Avatar answered Oct 21 '25 22:10

jasinth premkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!