Grabbing text and adding it to link - html

Is there a way to grab text from input area and put it to the end of "href" link and to "download=''" area? I added example below:
Code:
<input id="username" type="text" value="">
<a class="btn btn-block btn-lg btn-primary" href="http://mypage.com/test/(textarea text here)" download="(textarea text here).png">Download Image</a>
Result when user enter text:
<input id="username" type="text" value="Monster">
<a class="btn btn-block btn-lg btn-primary" href="http://mypage.com/test/Monster" download="Monster.png">Download Image</a>
Thanks for suggestions.

I've created a fiddle with a possible solution. might be minimized if you want to get rid of the variables declarations, but I like my code clean. https://jsfiddle.net/sctvdL0h/1/
Html
<input id="username" type="text" value="">
<a class="btn btn-block btn-lg btn-primary" href="" download="(textarea text here).png" id="download_link">Download Image</a>
JS
var input = document.getElementById("username");
var downloadLink = document.getElementById("download_link");
var baseLink = 'http://mypage.com/test/';
var baseFormat = '.png';
var updateLink = function() {
var currentValue = input.value;
downloadLink.href = baseLink + currentValue.toString();
downloadLink.setAttribute('download', currentValue.toString() + baseFormat);
};
input.addEventListener("keyup", function() {
updateLink();
});
input.addEventListener("change", function() {
updateLink();
});
In this way you can do it configurable (change url and file extension also). This is just a possible solution not the only one.

Add id of 'mylink' to your anchor tag.
<a id="mylink" class="btn btn-block btn-lg btn-primary" href="http://mypage.com/test/Monster" download="Monster.png">Download Image</a>
Then this code will set it. You will need to decide which event you want to trigger the setting of the href. onblur is a common one.
document.getElementById("mylink").href=document.getElementById("username").value;

You can use Javascript code. i give u an exemple.
<script>
(function ready() {
var input = document.getElementById('username'),
link = document.getElementById('getLink');
link.onclick = function() {
link.download = input.value + ".png";
window.open('http://mypage.com/test/' + input.value,'_blank');
}
})();
</script>
<input id="username" type="text" value="">
<a id="getLink" class="btn btn-block btn-lg btn-primary" href="">Download Image</a>

Related

Using checkbox to display or hide button in Angular

I am trying to display or hide buttons based on the checkbox. Following is my HTML code :
<input class="form-check-input" [(ngModel)]="switchCase" type="checkbox" id="flexSwitchCheckChecked" (change)="toggleButton()"
<button class="btn btn-primary" *ngIf="!switchCase" [disabled]="!userform.valid">Button A</button>
<button class="btn btn-primary" *ngIf="switchCase" [disabled]="!userform.valid">Button B</button>
Following is my TS code :
toggleButton(){
console.log("Before : "+this.switchCase);
this.switchCase = !this.switchCase;
console.log("After : "+this.switchCase);
}
The value of switchCase changes according to the checkbox value and logs correctly in the console. However, the buttons do not toggle accordingly and I can only see Button A. I am new to angular and not sure where I am going wrong
.html Code
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked"
(change)="toggleButton()">
<button class="btn btn-primary" *ngIf="!switchCase">Button
A</button>
<button class="btn btn-primary" *ngIf="switchCase">Button
B</button>
.ts code
public switchCase: boolean = false;
toggleButton() {
this.switchCase = !this.switchCase;
}

add dynamic ids using $scope variables in dynamically added html elements

Please see my code below and help me correct what I'm doing wrong:
$scope.more_work_counter=0;
$scope.appendWork = function(){
$scope.more_work=$sce.trustAsHtml($scope.more_work+'<input type="text" class="form-control more-work-fields" id="{{more_work_counter}}"><button class="btn btn-danger del-more-work-btn">-</button>'+'</br>');
$scope.more_work_counter++;
}
Below is the image of my DOM along with IDs I want to add in the input fields:
Judging by your sample code, you may want to add more fields to display in your html. You can use ng-repeat directive, check this plunk I've made
controller code:
$scope.fields = [
{id: 1}
];
$scope.addField = function(){
$scope.fields.push({
id : $scope.fields.length
});
}
html
<div ng-repeat="field in fields">
<input type="text" class="form-control more-work-fields" id="{{$index}}">
<button class="btn btn-danger del-more-work-btn">-</button>
</div>
<button ng-click="addField()">add field</button>
ok i did this and it worked... the old technique used in php so many times
$scope.appendWork = function(){
$scope.more_work=$sce.trustAsHtml($scope.more_work+'<input type="text" class="form-control more-work-fields" id="'+$scope.more_work_counter+'"><button class="btn btn-danger del-more-work-btn">-</button>'+'</br>');
$scope.more_work_counter++;
}

How to accept only date as input field value for bootstrap datepicker in angular

html
<form name="eventInformation" id="eventInformation"><label class="required"> Occurence Date </label>
<p class="input-group">
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="formData_EventDetails.eventOccurDate"
is-open="popup[0].opened" datepicker-options="dateOptions" close-text="Close" required/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open(0)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
<div class="col-sm-2 pull-right">
<button class="btn btn-block btn-primary ebtn"
ng-disabled="eventInformation.$invalid"
ng-click="submit()">Save</button>
</div>
</form>
javascript
$scope.format = 'yyyy/MM/dd';
$scope.dateOptions = {
formatYear : 'yy',
startingDay : 1
};
$scope.popup = [];
for (i = 0; i < 10; i++) {
$scope.popup[i] = {
opened : false
};
}
$scope.open = function(i) {
$scope.popup[i].opened = true;
};
I have bootstrap datepicker element which i am trying to only accept date as input, but it accepts all kinds of inputs i.e. string, numbers etc. How can i limit so it only takes date as input
Since it's a input text box, You can't restrict the user to enter invalid characters(default behavior).
But you can stop the model update operation, when the user provides wrong date (for that you need to setup following attributes on your input element)
ng-model-options="{allowInvalid: false}"

disable the selected button

How to disable a button which is been selected. Suppose i have 2 buttons, "ON" & "OFF".
Now if the "ON" button is selected it shouldn't be allowed to select the "ON" button again until the "OFF button is being selected."
<form action="myclass.php" method="post">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default btn-xs myOnbutton">
// myOnbutton is the button name
<input type="radio" autocomplete="off"> ON
</label>
<label class="btn btn-default btn-xs myOffbutton">
// myOffbutton is the button name
<input type="radio" autocomplete="off"> OFF
</label>
</div>
</form>
So the main purpose is to disable the button which is selected.
Do anyone knows how to solve this problem !
Thanks in advanced.
Try this variant jsFiddle:
$(function () {
var onButton = $(".btn-group .btn:eq(0)");
var offButton = $(".btn-group .btn:eq(1)");
onButton.on("click", function () {
if (!onButton.is("disabled")) {
offButton.attr("disabled", false);
onButton.attr("disabled", true);
}
});
offButton.on("click", function () {
if (!offButton.is("disabled")) {
onButton.attr("disabled", false);
offButton.attr("disabled", true);
}
});
});
// https://jsfiddle.net/243mbpwo/2/
var inputs = document.getElementsByTagName("input");
var onButton = inputs[0];
var offButton = inputs[1];
onButton.addEventListener("click", function () {
this.setAttribute("disabled", "disabled");
});
offButton.addEventListener("click", function () {
onButton.removeAttribute("disabled");
});
Why dont you try this, if you are already using bootstrap - http://getbootstrap.com/javascript/#buttons-checkbox-radio
Write your code like this:
<div class="btn-group" data-toggle="buttons">
<input type="radio" name="toggle" value="on">ON</br>
<input type="radio" name="toggle" value="off">OFF</br>
</div>
You don't need javascript or jQuery for this approach.

HTML help --hidding a button

I need help with my HTML styling.
here is the HTML code:
<div id="hidden" style="display: none;">
<label>URL:</label>
<input type="text" name="url" size="50"/><br/>
<input type="button" id="button2" value="Update"/> <br/>
</div>
<input type="button" id="button1" value ="Get Info"
onclick="document.getElementById('hidden').style.display = '';" size="25"/>
As you can see, all the elements that are inside <div></div> will be displayed upon clicking button1 (they are hidden initially).
What I want is that when button1 is clicked in addition to all the other fields (including button2) being displayed, button1 to be hidden.
How can I do this?
Change
onclick="document.getElementById('hidden').style.display = '';"
to
onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'"
You can see this in action here: http://jsfiddle.net/nayish/sJ5RR/.
instead of '' change it to 'Block'
so:
<input type="button" id="button1" value ="Get Info"
onclick="document.getElementById('hidden').style.display = 'Block';" size="25"/>
But the best way will be to externalize your script so you can do more stuff without cluttering your html
var btn = document.getElementById('button1');
btn.onclick = function () {
// all the stuff you want to do
document.getElementById('hidden').style.display = "block";
this.style.display = "none";
... more stuff ...
}