1

I have two textboxes and one button, I want to add one new textfield, that should show card name from textbox1 and Link URL append from textbox2 when I click on button

//AddnNewCardNavigator
var counter=2;
var nmecardtxt= document.getElementById("textbox1").value;
var linkurltxt= document.getElementById("textbox2").value;
$("#addbutton").click(function(){
  if(nmecardtxt ==""||nmecardtxt ==0||nmecardtxt ==null 
  && linkurltxt ==""||linkurltxt ==""|| linkurltxt ==0||linkurltxt ==null){
    alert("Please insert value in Card name and Link Url textboxes and must be correct");
    return false;
  }
var NewCarddiv = $(document.createElement('div')).attr("id",'cardlink'+counter);
NewCarddiv.after().html()
})
</script>
<!-- text boxes-->
<div class="row">
<div class="col-md-12">
  <div id="textboxesgroup">
    <div id="textboxdiv1">
      <label style="color:blanchedalmond">Card Name: </label><input type="textbox" id="textbox1">
    </div>
    <div id="textboxdiv2">
      <label style="color:blanchedalmond">Link Url:&ensp;&ensp;&ensp; </label><input type="textbox" id="textbox2">
    </div>
  </div>
</div>
</div>

2
  • 1
    The "button" isn't in your snippet. Commented May 22, 2018 at 14:04
  • I have done some changes in your question as per your description. Please check if that's your actual query Commented May 22, 2018 at 14:07

2 Answers 2

1

Your variables nmecardtxt and linkurltxt must be created inside the click function,
because it's empty at the loading of the page.

I also took the liberty to use jQuery for that variables, as you're already using it, and tried to enhance some other things:
(See comments in my code for details)

//AddnNewCardNavigator
var counter = 2;

// On click function
$("#addbutton").click(function() {

  // Here it's better
  var nmecardtxt = $("#textbox1").val(); 
  var linkurltxt = $("#textbox2").val();

  // Modified you test here
  if (!nmecardtxt || !linkurltxt) {
    alert("Please insert value in Card name and Link Url textboxes and must be correct");
    return false;
  }
  
  // Modified creation of the card
  var link = $(document.createElement('a')).attr("href", linkurltxt).html(linkurltxt);
  var NewCarddiv = $(document.createElement('div')).attr("id", 'cardlink' + counter).html(nmecardtxt + ": ").append(link);
  $('#cards').append(NewCarddiv);
  //NewCarddiv.after().html(); // Was that line an attempt of the above ?
});
body {
  background: #888;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- text boxes-->
<div class="row">
  <div class="col-md-12">
    <div id="textboxesgroup">
      <div id="textboxdiv1">
        <label style="color:blanchedalmond">Card Name: </label><input type="textbox" id="textbox1">
      </div>
      <div id="textboxdiv2">
        <label style="color:blanchedalmond">Link Url:&ensp;&ensp;&ensp;</label><input type="textbox" id="textbox2">
      </div>
    </div>
  </div>
</div>

<!-- Added the below -->
<div id="cards">
</div>
<button id="addbutton">Add…</button>

Hope it helps.

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

Comments

0

Here's a simplified version of what you're trying to accomplish:

function addNewCard() {
  var name = $('#name').val();
  var url = $('#url').val();
  var count = $('#cards > .card').length;
  
  if (!name || !url) {
    alert('Missing name and/or URL.');
  }
  
  var card = $('<div class="card"></div>').html("Name: " + name + "<br>URL: " + url);
  
  $("#cards").append(card);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="url">URL:</label>
<input type="text" id="url" name="url">
<input type="submit" value="Add Card" onclick="addNewCard();">

<div id="cards">
</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.