0

I have upated the question now. Below is the code which i stored in the external js file.

$js ='

var i=1;
var siteUrl = "http://'.$_SERVER['SERVER_NAME'].'";

$jd = jQuery.noConflict();
function dialog(urlvalue){
    $jd( "#dialog" ).dialog();
    alert("+urlvalue+");
}
$jd(document).ready(function(){
    var id = $jd(".product");

    id.each(function(index,element){
        //var productUrl = $jd(".product").attr("href");
        var productUrl = $jd("a:first-child ", this).attr("href");
        if(productUrl != undefined){
             //console.log(productUrl);
             productUrl = siteUrl + productUrl ; 
             //$jd(this).addClass("item-" + i);
             //console.log("checking"+index);
             $jd(".vm-product-media-container").addClass("view view-first");
             //console.log($jd(".js-recalculate input[name=pid]").val());
             $jd(".view-first", this).append("<button onclick=\"dialog("+productUrl+")\" class=sdlc_quick> Quick View</button>");       
             //console.log(productUrl);
         }
    });
})';
$doc->addScriptDeclaration($js);

I am getting an error missing argument when i clicked on the button quick view. Below is the code:-

$jd(".view-first", this).append("<button onclick=\"dialog("'+productUrl+'")\" class=sdlc_quick> Quick View</button>");
8
  • Remove the quotes in the alert . Commented Aug 10, 2015 at 7:54
  • It is still showing the error Commented Aug 10, 2015 at 8:00
  • I can’t find any errors with JSHint as well. Maybe the error is at some other location. Do you have more JavaScript code in your PHP? Commented Aug 10, 2015 at 8:01
  • Apart from this script i have added only external js $doc->addScript('//code.jquery.com/ui/1.11.4/jquery-ui.js'); Commented Aug 10, 2015 at 8:05
  • 1
    You don't wrap your variable between quotes when you are calling the dialog() function. Commented Aug 10, 2015 at 8:07

1 Answer 1

2

Give this a try:

http://jsfiddle.net/7uyhr0vh/1/ (tried to understand the html structure, hope i got it right)

I mainly played with quotes and added a click event listener to the 'sdlc_quick' class

var i=1;
var siteUrl = "http://google.com";

$jd = jQuery.noConflict();

$jd(document).ready(function(){

    function dialog(urlvalue){
        $jd( "#dialog" ).dialog();
        alert(urlvalue);
    }

    var id = $jd(".product");
    id.each(function(index,element){
        var productUrl = $jd("a:first-child ", element).attr("href");
        if(productUrl != undefined){
            productUrl = siteUrl + productUrl ; 
            var x = $jd(".vm-product-media-container");
            x.addClass("view view-first");
            console.log(x)
            $jd(".view-first").append("<button class=\"sdlc_quick\"> Quick View</button>");
            $jd(".sdlc_quick",this).click(function(){
                dialog(productUrl);
            });
         }
    });
})
Sign up to request clarification or add additional context in comments.

3 Comments

Well your answer was correct. i added it on my code and it was working correctly except one thing i am getting all the url in the alert. so i only modified one line:-$jd(".sdlc_quick",this).click(function() and it works fine
Updated my answer to include your observation. Thanks!
Thanks for your help. now i able to understand and solved my issue

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.