Jquery Search Text From Page With Next And Previous Button Example

 next and previous button jquery

Hi Guys,

Today, I will learn you how to search text from page with next and previous button useing jquery.we will show example of search text. you can easliy search text from page with next and previous button useing jquery.i will use highlight js to make jquery search text from page with next and previous button.

Here, I will give you full example for simply search text from page with next and previous button using jquery as bellow.

Example
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script type='text/javascript' src='https://johannburkard.de/resources/Johann/jquery.highlight-4.js'></script>
<style type='text/css'>
    body {
        font-family:Calibri,Tahoma,Verdana; 
        border:2px solid #000;
        padding: 10px;
        text-align: center;
    }
    input {
        font-family: inherit; 
    }
    input[type=button] {
        min-height:24px; min-width:64px; 
    }
    body p {
     font-family:Consolas; text-align:justified; margin: 10px; 0 0 0;
    }
    .highlight {
     background-color:yellow; 
    }
    .emptyBlock1000 {
     height:auto; 
    }
    .emptyBlock2000 {
     height:auto; 
    }
    .current {
     background-color: #957F68 !important; color: #ffffff; 
    }
</style>
</head>
<body>
    <br/>
        <h2>Jquery Search Text From Page With Next And Previous Button - Itwebtuts</h2>
    <form name="frmMain" id="frmMain" method="post">
        <input type="text" id="searchTerm" />
        <input type="button" id="btnNext" value="next" />
        <input type="button" id="btnPrev" value="prev" />
    </form><br/>
    <div id="bodyContainer">
        <p class="emptyBlock1000">
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere<br>
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Enim repudiandae deserunt, voluptatum dignissimos. Sint repellat dolore rem, sapiente quis ut, soluta ipsam repellendus, adipisci laudantium odit ipsa quidem quo molestiae!
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere
        </p>
    </div>
</body>
<script type='text/javascript'>
var lstEl = null;
var cntr = -1;

$(document).ready(function() {
    $('#searchTerm').keyup(function() {
        lstEl = null;
        cntr = -1;
        var vl = document.getElementById('searchTerm').value;
        $("#bodyContainer").removeHighlight();
        $("#bodyContainer").highlight(vl);
    });

    $('#btnNext').click(function() {
        if (lstEl === null) {
            lstEl = $('#bodyContainer').find('span.highlight');
            if (!lstEl || lstEl.length === 0) {
                lstEl = null;
                return;
            }
        }
        if (cntr < lstEl.length - 1) {
            cntr++;
            var cntrlast = cntr -1;
            if (cntr > 0) {
                $(lstEl[cntrlast]).removeClass('current');
            }

            var elm = lstEl[cntr];
            $(elm).addClass('current');
        } else
            alert("End of search reached!");
    });

    $('#btnPrev').click(function() {
        if (lstEl === null) {
            lstEl = $('#bodyContainer').find('span.highlight');
            if (!lstEl || lstEl.length === 0) {
                lstEl = null;
                return;
            }
        }
        if (cntr > 0) {
            cntr--;
            if (cntr < lstEl.length) {
                $(lstEl[cntr + 1]).removeClass('current');
            }
            var elm = lstEl[cntr];
            $(elm).addClass('current');
        } else
            alert("Begining of search!");
    });
});
</script>
</html>

It will help you...

Laravel One to Many Eloquent Relationship Tutorial

laravel one to Many eloquent relationship

Hi Dev,

In this tutorial, I Will explain you how to create laravel one to Many eloquent relationship. We will create one to many relationship in laravel. We will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc. I will show you laravel hasMany relationship example.

We will create "employee" and "salary" table.both table are connected with each other, I will create one to many relationship with each other by using laravel Eloquent Model, We will first create database migration, then model, retrieve records and then how to create records too.One to Many Relationship use "hasMany()" and "belongsTo()" for relation.

Here, I will give you full example for laravel one to many eloquent relationship as bellow.

Step:1 Create Migration

In this example, we will need two tables in our database employee and salary.I will start to create the model and migrations, then we will define the one to hasMany relationship.To generate models and migrations, run below two commands in your terminal.

php artisan make:model Employee -m

php artisan make:model Salary -m

Above command will generate two models in your app folder called Employee and Salary. You will also find two new migrations in your database/migrations folder.

Your newly created models and migration after add table field: Path:database\migrations\2014_10_12_000000_create_employee_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEmployeeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employee', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('salary_id');
            $table->integer('extra_salary_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employee');
    }
}
Path:database\migrations\2014_10_12_000000_create_salary_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSalaryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('salary', function (Blueprint $table) {
            $table->id();
            $table->integer('amount');
            $table->date('payment_date');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('salary');
    }
}
Now run the below command to create employee and salary tables in your database:
php artisan migrate
Step:2 Create Model Relationship

Now in this step, we have employee and Salary tables ready, let’s define the one to manMany relationship in our models. our app/Employee.php model file and add a new function called salary() which will return a hasMany relationship like below:

app\Employee.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = [
        'name','salary_id','extra_salary_id'
    ];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function salary()
    {
        return $this->belongsTo(Salary::class);
    }
}

you can see, in the salary() method we are defining a hasMany relationship on Salary model.

Now we will defining Inverse One To Many Relationship in Laravel.As we have defined the hasMany relationship on Brand model which return the related records of Salary model, we can define the inverse relationship on the Salary model.Open your app/Salary.php model file and add a new method called employee() in it which will return the related brand of a Salary.

app\salary.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Salary extends Model
{
   /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = ['amount','payment_date'];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function employer()
    {
        return $this->hasMany(Employer::class);
    }
}

As you can see, in employee() method we are returning a belongs to relation which will return the related employee of the salary.

Step 3 : Insert Records

Let’s in this step add data to employee and salary from controller:

app\Http\Controllers\Employee.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function Employee()
    {   
        $employee = employee::find(1);
        $employee->name = "Test Name";
        $employee->salary_id ="1"
        $employee->extra_salary_id ="2"
        
        $employee = $employee->salary()->save($employee);
    }
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function Salary()
    {
        $employee = employee::find(1);

        $salary1 = new Salary;
        $salary->amount = '123456789';
        $salary->payment_date = '15/07/2020';
        
        $salary2 = new Salary;
        $salary->amount = '123456789';
        $salary->payment_date = '16/07/2020';
        
        $employee = $employee->salary()->saveMany([$salary1, $salary2]);

    }
}
Step 4 : Retrieve Records

Now in this step retrieve records model

app\Http\Controllers\Employee.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::find(1);
 
        $employee = $employee->salary;
 
        dd($employee);

        $salary = Salary::find(1);
 
        $salary = $salary->employee;
         
        dd($salary);
    }
}
it will help you....