I have been attempting to implement validation in JQuery, but it doesn't seem to be functioning correctly

Can someone assist me with adding validation in jQuery? I'm stuck and need help solving this problem.

$(function(){

        $("#btn").click(function(){
        var b=prompt("Enter your link");
        $("a").attr("href",b);      
        if($("b").val()=='')
        {
            var con=confirm("Please Enter your link");
            if(con==true)
            {
                var b=prompt("Enter your link");
                $("a").attr("href",b);

            }else
            {
                var pr=confirm("Please Enter your link");
            }

        }

    });

The provided html code is also shown below:

<html>
<head>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
</head>
<body>
        <div id="div1">
                <a href="https://www.facebook.com"><h3>Facebook</h3></a>

                <img src="1.jpg" alt="My Images">

        </div>
<button id="btn">Attribute(a)</button>
</script>
</body>
</html>

I'm eagerly waiting for solutions, thank you.

Answer №1

<html>
  <head>
  </head>
  <body>
    <div id="div1">
      <a href="https://www.facebook.com"><h3>Facebook</h3></a>
      <img src="1.jpg" alt="My Images">
    </div>
    <button id="btn">Attribute(a)</button>
    <script type="text/javascript" src="jquery-3.3.1.js"></script>
  </body>
</html>

/////////////////////

$(function(){
  $('#btn').on('click',function(){
      var b=prompt("Enter your link");
      var link = $("a").attr("href",b);
    if(link.val()==''){
       var con=confirm("Please Enter your link");
       if(con==true){
           $("#btn").trigger('click');
       }
    }
  });
});

Answer №2

Let's take a look at this piece of code... if I comprehend correctly

you need to modify $("b").val()=='' because b is not an HTML element, but rather a parameter

Additionally, it would be more elegant to use onclick="func()" rather than

$('#btn').on('click',function(){...}
, however, the choice is yours

function func(){
        var b=prompt("Enter your link");
        $("a").attr("href",b);      
        if(b=="")
        {
            var con=confirm("Please Enter your link","");
            if(con!="")
            {
                var b=prompt("Enter your link");
                $("a").attr("href",b);

            }else
            {
                var pr=confirm("Please Enter your link");
            }

        }
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
  <a href="https://www.facebook.com"><h3>Facebook</h3></a>
  <img src="1.jpg" alt="My Images">

</div>
<button onclick="func()" id="btn">Attribute(a)</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to resize a PDF to match the width and height of a canvas using

I need help with a project involving rendering previews of PDF documents in a UI similar to Google Docs. {documents.map((doc) => { return ( <div key={doc.id} className=" ...

Switch up the Javascript popup code without any specific pattern

I am looking to add variety to my pop up ads on my website by randomly changing the Javascript code for each ad every time a page is loaded. How can I achieve this? Script 1 <script type="text/javascript"> var uid = '12946'; var w ...

Learn the process of using calc to rotate images on hover with CSS and Jquery

Is there a way to implement the rotate on hover function for images, similar to what is seen on this website under 'our Latest Publications'? I attempted using calc and skew, however, I was unsuccessful in achieving the desired hovering effect o ...

SharePoint 2013 only allows the first AJAX request to work successfully, except when in Edit Mode

I am facing a challenge of making two AJAX requests simultaneously on a SharePoint page. One request originates from the current site, while the other comes from a team site. When I use different URLs (/events) and (/travel), each one works fine when use ...

Updating select option values without reloading the page using a modalHere is an efficient method for updating

Is there a way we can update the select tag option values without having to reload the page after submitting data in the bootstrap modal? Currently, we are facing an issue where the submitted values do not show up in the option tag until the page is reload ...

Seeking help with executing JQuery Ajax functions within a foreach loop

Currently, I am engrossed in the study of programming and endeavoring to construct a website utilizing .Net Core. The predicament at hand pertains to my limited acquaintance with JavaScript while incorporating two JQuery/AJAX functions on my Index page - o ...

Activate jquery script upon initial page load, and refrain from doing so thereafter for that specific user

Recently, I integrated a jQuery Modal Window script into my website from Currently, the Modal Window is triggered when a user clicks on a link. However, I am looking to customize it so that it automatically opens upon the initial page load but does not ac ...

Loop through a non-array or non-object / handling both arrays and non-arrays equally

Sometimes, I find myself needing to handle arrays and single objects in a similar manner. For instance, I may have an object property that can be either an array or just a string (like the scale property): [ { "name": "Experiment type14", "id": ...

Improving the efficiency of your if else code in React js

I am looking for a way to optimize my code using a switch statement instead of multiple if-else conditions. How can I achieve this? Here is the current version of my code: handleChange = (selectedkey) => { this.setState({ activeKey: selectedkey } ...

Exploring the methods for retrieving and setting values with app.set() and app.get()

As I am granting access to pages using tools like connect-roles and loopback, a question arises regarding how I can retrieve the customer's role, read the session, and manage routes through connect-roles. For instance, when a client logs in, I retrie ...

Sending data from an AJAX POST request to a Grails Controller

Currently, I am in the process of developing a social networking platform using Grails. However, I have encountered a roadblock when it comes to allowing users on their edit profile page to input a YouTube URL into a text field. By clicking a button, a Jav ...

What is the reason behind the addition of right padding to texts containing non-ASCII characters?

Upon observation of the image below, it is evident that text containing non-ASCII characters tends to be pushed away from the right margin, while text with emojis is pushed closer to the margin. Have you ever wondered why this happens? https://i.stack.img ...

Does the first Ajax call always finish first in the order of Ajax calls?

In my code, I have an ajax call that triggers another ajax call based on its return value. The URL parameter of the second call is modified by the output of the first one. These two calls are interrelated as the first call feeds the URL parameter for the s ...

Header 1 is not receiving any special styling, while Headers 2 through 6 are being styled accordingly

I am currently utilizing Skeleton V2.0.4 to design a landing page, but I seem to be having trouble with my styles not displaying correctly. Below are the specific lines of code related to it: /* Typography –––––––––––––– ...

Issues have been encountered with the functionality of $rootScope

I am currently struggling with a code snippet in my loginCtrl.js file where I can't seem to get $rootScope to store the value of vm.userEmail. app.controller('LoginCtrl', function($timeout, $q, $log, $rootScope /*$auth*/, $location, $mdTo ...

Obtain content from a div element using jQuery

Here is a snippet of HTML code that I am working with: <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> </head> <body> <script src="jquery.js"></script> <scri ...

Discover the secrets to acquiring cookies within a Next.js environment

I am currently working with Next.js and attempting to retrieve a cookie value. Below is the code I have written: import cookie from "js-cookie"; export default function Home({ token }) { return ( <div className="flex flex-col items ...

Obtaining the NodeValue from an input of type <td>

I have a HTML code snippet that I am trying to parse in order to extract the nodeValue of all elements within the table columns. <table id="custinfo"> <tr> <td><label>First Name</label></td> <td& ...

Firefox does not display the line headings on the sides

Hey everyone, I'm facing a compatibility issue with CSS and Firefox today. When I go to my website www.fashoop.com, you'll notice some header lines on the sides. Here's an example in Google Chrome: (GOOGLE CHROME EXAMPLE) COLOR BLUE AN ...

Having trouble converting svg to png, thinking it might be due to discrepancies in svg elements

I am facing a puzzling issue where two different SVG elements are causing my JavaScript to work in one scenario but not the other. I have replaced the SVG elements in both examples, and surprisingly, only one works while the other does not. You can view th ...