1

I have an html page with several hyperlinks on it that share the same id but have a custom property linkID that differs. I know it is not a good practice but that is not something I can change at this point. I need to open a modal dialog box whenever a link is clicked. For that I am using JQuery. Now I need to pass the linkID property to the Jquery function. I tried using $("#hlk1").attr("LinkID") and $(this).attr("LinkID") but neither worked. It comes up as "undefined".

<html>
<head>
<script src="Script\jquery-2.2.0.js"></script>
<script src="Script\jquery-ui-1.8.24.js"></script>
<script>
$( document ).ready(function() {
    $("#hlk1").click(function(){ 
        var linkId=$("#hlk1").attr("LinkID");
        // initialize dialog
        var dlg = $("#dialog").dialog({ 
            autoOpen: false, height: 500, width: 350
        });

        // load content and open dialog
        dlg.load('page2.html?id=' + linkId).dialog('open');
    });
});

 </script>

</head>
<body>
<a href="#" id="hlk1" linkID="305">Click here</a>
<a href="#" id="hlk1" linkID="890">Click here</a>
<div id="dialog">

</div>
</body>
</html>
3
  • 2
    Stopped reading at "...share the same id" <- that's an error! Commented Feb 8, 2016 at 20:49
  • Check your case. You have linkID with a lowercase "L" in the a tag but LinkID in your jQuery has an uppercase "L". Commented Feb 8, 2016 at 20:49
  • Don't use custom attributes. Use data-linkID intead of linkID. Commented Feb 8, 2016 at 21:24

3 Answers 3

3

You need to use linkID not LinkID:

var linkId= $("#hlk1").attr("linkID");

You should also take advantage of using the data attributes.

As an additional note you may have issues with selecting multiple elements with the same identifier with jQuery, therefore it may be better to use a class and attach a handler.

HTML:

<a href="#" class="hlk1" linkID="305">Click here</a>
<a href="#" class="hlk1" linkID="890">Click here</a>

jQuery:

$(".hlk1").on("click", function() {
   var linkId = $(this).attr("linkID");
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can't repeat ID's in a page so use class instead. Then inside the click handler this is the element that event occurred on

<a href="#" class="hlk1" linkID="305">Click here</a>
<a href="#" class="hlk1" linkID="890">Click here</a>

JS

 $(".hlk1").click(function(){ 
        var linkId=$(this).attr("linkID");
        // initialize dialog
        var dlg = $("#dialog").dialog({ 
            autoOpen: false, height: 500, width: 350
        });

        // load content and open dialog
        dlg.load('page2.html?id=' + linkId).dialog('open');
    });

Comments

0

You need the .data() method. Also use data-attributes properly (data-linkId = "305")

$( document ).ready(function() {
    $("#hlk1").click(function(){ 
        var linkId=$(this).data("LinkID");
        // initialize dialog
        var dlg = $("#dialog").dialog({ 
            autoOpen: false, height: 500, width: 350
        });

        // load content and open dialog
        dlg.load('page2.html?id=' + linkId).dialog('open');
    });
});

1 Comment

$(this).data("LinkID") still came up as undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.