Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:42:56+00:00 2026-06-15T19:42:56+00:00

I have a modal box in jQuery which I have created to display some

  • 0

I have a modal box in jQuery which I have created to display some embed code. I want the script to take the id of the link that is clicked but I can’t seem to get this working.

Does anyone know how I can do that or why this may be happening?

My jQuery code is:

function generateCode() {
    var answerid = $('.openembed').attr('id');
    if($('#embed input[name="comments"]:checked').length > 0 == true) {
        var comments = "&comments=1";
    } else {
        var comments = "";
    }
    $("#embedcode").html('<code>&lt;iframe src="embed.php?answerid=' + answerid + comments + '" width="550" height="' + $('#embed input[name="size"]').val() + '" frameborder="0"&gt;&lt;/iframe&gt;</code>');
}

$(document).ready(function () {
    $('.openembed').click(function () {
        generateCode();
        var answerid = $('.openembed').attr('id');
        $('#box').show();
        return false;
    });
    $('#embed').click(function (e) {
        e.stopPropagation()
    });
    $(document).click(function () {
        $('#box').hide()
    });
});

My mark-up is:

<a href="#" id="7830" class="openembed">Embed</a>
<a href="#" id="9999" class="openembed">Embed</a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:42:57+00:00Added an answer on June 15, 2026 at 7:42 pm

    Your problem is here:

    $('.openembed')
    

    returns an array of matched elements. Your should instead select only the clicked element.
    $('.openembed') works correctly if you assing a click event to all elements that have this class. But on the other hand, you’re unable do know which is clicked.

    But fortunately in the body of handler function click you could call $(this).

    $(this) will return the current (and clicked element).

    // var answerid = $('.openembed').attr('id'); // Wrong
    var answerid = $(this).attr('id');   // Correct
    // Now you can call generateCode
    generateCode(answerid);
    

    Another error is the body of generateCode function. Here you should pass the id of selected element. This is the correct implementation.

    function generateCode(answerid) {
        if($('#embed input[name="comments"]:checked').length > 0 == true) {
            var comments = "&comments=1";
        } else {
             var comments = "";
        }
        $("#embedcode").html('<iframe src="embed.php?answerid=' + answerid + comments + '" width="550" height="' + $('#embed input[name="size"]').val() + '"frameborder="0"></iframe>');
    }
    

    Here I have implemented your code with the correct behavior: http://jsfiddle.net/pSZZF/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to raise a jQuery Dialog box which has 2 buttons. So that
I have a Jquery Dialog box which I want to open as soon as
Problem: Display jquery flot graph in dialog box I have a page which displays
I have a Modal Box that opens by clicking a HyperLink. In the Modal
I have a datagrid column with a button that opens a modal dialog box
I have a JInternalFrame window that needs to popup a modal dialog box when
What is wrong with this login script? I have a modal-style iframe login box
I am using the jquery dialog widget to display a modal box. However when
I have a Jquery Dialog box and the code is below, $(document).ready(function() { $(#dialogForm).dialog({
I currently have a jQuery function which onclick opens a dialog box. This allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.