0

I want to pre - populate text box (and gray it out for editing) with a URL parameter value inside a jquery snippet of code:

The Jquery code:

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="email">\
                <input class="form-control" type="text" name="text" placeholder="@domain">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
  

The specific text field i want to prepopulate :

<input class="form-control" type="text" name="domain" placeholder="@domain">\

The function i have for obtaining the said parameter value:

function GetURLParameter(sParam)

    {

        var sPageURL = window.location.search.substring(1);

        var sURLVariables = sPageURL.split('&');

    for (var i = 0; i < sURLVariables.length; i++)

    {

        var sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] == sParam)

        {

            return sParameterName[1];

        }

    }

}​

7
  • I don't see a specific question or a description for a specific issue. Could you please clarify what exactly is not working as you expect? Commented Aug 4, 2020 at 18:58
  • I need to make the value of the 'domain' field a URL parameter value in other words the URL: test.com/?domain=mydomain I need to know how to prepupolate that inside the first snippet of the JQUERY code Commented Aug 4, 2020 at 19:04
  • i used the following value="${domain}" but the text box only just contains that exact same text and doesnt evaluate to the value of what 'domain' is Commented Aug 4, 2020 at 21:02
  • You have the syntax incorrect for expressions in strings. You want, value =`${domain}`. But not sure you wouldn't just write, value = domain if domain is a string. But I'd need to see more context. Commented Aug 4, 2020 at 21:49
  • There are many duplicates here on SO, have you tried those? stackoverflow.com/questions/50695292/…, stackoverflow.com/questions/3680060/…, stackoverflow.com/questions/16188151/… ... Commented Aug 4, 2020 at 23:47

2 Answers 2

0
$('#selector').val('value you want to input here')

https://api.jquery.com/val/

Sign up to request clarification or add additional context in comments.

Comments

0

It turns out the solution is using backticks on the html snippet of coded within the JQUERY like this:

        $(".inc").append(`<div class="controls">\
                <input class="form-control" type="text" id="email" name="email" placeholder="email">\
                <input class="form-control" type="text" id="domain" name="domain"  value="${domain}">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>`
           
            );

Comments

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.