John Davidson

php - How to call a Javascript function in an attribute of an element

0 comments
Message:


I'm working in laravel php and have blade.php views,


I have the following component which is used in blade.php as follows


<x-form.select
wire:model.defer="state.trip_destination"
:search="true"
x-search="
(query) => {
return new Promise((resolve, reject) => {
resolve(fun()); //HERE
});
}
"
name="state.trip_destination"
type="text"
id="trip-destination"
/>

Now I want to call the function fun() which is just returning a string, and is defined as


<script>
function fun()
{
return "HELEL";
debugger;
};
</script>

It doesn't goes to that function because it is out of scope, is there any way to solve this problem.


Thanks,



My Answer:
One way to solve this problem is to define the `fun()` function within the `x-form.select` component itself. This way, the function will be in scope when it is called in the `x-search` attribute.

Here's how you can modify your component to include the `fun()` function:

php
wire:model.defer="state.trip_destination"
:search="true"
x-search="
(query) => {
return new Promise((resolve, reject) => {
resolve(fun());
});
}
"
name="state.trip_destination"
type="text"
id="trip-destination"
>




By including the `fun()` function within the component, it will be in scope when called in the `x-search` attribute. This should allow the function to be called successfully.

Rate this post

3 of 5 based on 5895 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us