Henrik Baecklund
Henrik Baecklund

Reputation: 43

Fetch email within variable then push event onclick

Need to push qg("identify", {"email": "<email address of the user"}); when email form submit button is clicked. I need to push in the code from a third party solution where I can only use html/css/javascript.

Here i insert the jquery ui popup form

$.ajax({
  url: '//code.jquery.com/ui/1.12.1/jquery-ui.js',
  dataType: 'script',
  cache: true,
  success: function() {
    $("#dialog").dialog();
  }
});

Then i insert the jquery ui css in head

$("<link/>", { rel: "stylesheet", type: "text/css", href: "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"}).appendTo("head");

Then I push the popup onto body

$("body").append('<div id="dialog" title="BEVAKA PRODUKT"><div id="dialog-form" title="Create new user"><p class="validateTips">Här kan du skriva in din e-post och få ett mail när produkten finns i lager igen</p><form><fieldset><label for="email">E-postadress</label><input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all"><input type="submit" tabindex="-1" style="position:absolute; top:-1000px"></fieldset></form></div><button id="create-user">Bevaka</button></div>');

Her I fetch email adress in var emailTest

var emailTest = $("#email").val();

Here I alert the emailTest variable on click

$("#create-user").click(function(){
    alert(emailTest);
});

This is what needs to be pushed when email form is submitted

//qg("identify", {"email": "<email address of the user"});

Protoype

var email = $("#email").val();

$("#create-user").click(function(){
    qg("identify", {"email": "email"});
})

Upvotes: 1

Views: 323

Answers (1)

user1741851
user1741851

Reputation:

As I mentioned earlier, javascript / jquery is event based.

First of all,

$.ajax({
  url: '//code.jquery.com/ui/1.12.1/jquery-ui.js',
  dataType: 'script',
  cache: true,
  success: function() {
    $("#dialog").dialog();
  }
});

You need to ensure that #dialogue exists before you call dialog on it. You have added the script to append the #dialogue div to body directly which will be triggered when the current script loads.

Similarly, when you add the code

var email = $("#email").val();

The variable will be assigned a value when current script loads. At that point of time, your email field is obviously empty.

You want to get the value of #email field when button is clicked. So move that code inside the click handler.

Upvotes: 1

Related Questions